Spring.Net IoC和魔术字符串 [英] Spring.Net IoC and Magic Strings

查看:47
本文介绍了Spring.Net IoC和魔术字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑IApplicationContext.GetObject(string name)方法,它似乎在很大程度上依赖魔术字符串从应用程序上下文中获取对象.使用 StructureMap 之类的其他容器,您可以使用泛型来指定配置和请求对象,但是是否存在比在Spring.Net IoC容器中使用字符串从ApplicationContext请求对象更好的方法?

I've been thinking about the IApplicationContext.GetObject(string name) method and it seems to rely fairly heavily on magic strings to get objects from the application context. With other containers like StructureMap you can use generics to specify the configuration and request objects but is there a better way than using strings with the Spring.Net IoC container to request objects from the ApplicationContext?

推荐答案

您可以使用通用类型参数调用包装器.

You could have a wrapper that you call, taking a generic type parameter.

类似这样的东西:

public void MyMethod()
{
    IMyService myService = ApplicationContextWrapper.Resolve<IMyService>();
}


public static class ApplicationContextWrapper
{
    public static T Resolve<T>()
    {
        return ApplicationContext.Resolve<T>(typeof(T).Name);
    }
}

不如其他一些IoC竞争器好,但是至少您会获得某种编译器支持.

Not as good as some of the other IoC contatiners, but at least you will get some kind of compiler support.

这篇关于Spring.Net IoC和魔术字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆