我应该使用Unity Config文件还是Code来注册类型和实例? [英] Should I use Unity Config file or Code to register types and instances?

查看:104
本文介绍了我应该使用Unity Config文件还是Code来注册类型和实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后开始配置IoC容器!

Finally started to configure an IoC Container!

我正在使用Unity并将其配置为使用配置文件注册我的对象:

I'm using Unity and have configured it to have my objects registered using the config file:

例如

      <container>
        <register type="ILogger" mapTo="Logger">
          <lifetime type="singleton"/>
        </register>
        <register type="IPdfWriter" mapTo="PdfWriter">
          <lifetime type="perthread" />
          <constructor />      
        </register>
</container>

我已经怀疑我是否是注册类型的好方法.

I've reached a point where I doubt this is a good approach to register types.

例如,我的一个类依赖于Microsoft Enterprise Library Caching块中的ICacheManager,并且应将以下内容注入到其中:

For example a class of mine is dependent on the ICacheManager from Microsoft Enterprise Library Caching block and the below should be injected to it:

EnterpriseLibraryContainer.Current.GetInstance<ICacheManager>()

而且似乎仅使用统一配置设置是不可能的.

and that doesn't seem to be possible using just unity config settings.

在我看来,总是最好使用代码而不是配置文件来注册类型.

It seems to me that always it's better to register the types using code rather than the config file.

您对此有何经验/建议?

What's your experience/advise in this?

谢谢

推荐答案

答案是-您认为部署后需要更改配置吗?如果是这样,请使用配置文件.如果没有,我发现用代码做起来会更容易.

The answer is - do you think you'll need to change the configuration after deployment? If so, use the config file. If not, I find doing it in code to be easier.

回答您没有问的问题-您实际上想做的事很容易,而且很容易.诀窍是将相同的容器实例用于企业库和其他所有内容.在启动代码中,设置您的Unity容器实例,然后向其中添加EnterpriseLibraryCoreExtension(您可以在config中进行此操作).然后将该容器设置为EnterpriseLibraryContainer.Current.完成此操作后,您就可以拥有一个依赖于ICacheManager的类型,其他所有内容都将正常工作.

And to answer the question you didn't ask - what you want to do is in fact possible, and quite easy. The trick is to use the same container instance for Enterprise Library and for everything else. In your startup code, set up your Unity container instance, and add the EnterpriseLibraryCoreExtension to it (which you can do in config). Then set that container as your EnterpriseLibraryContainer.Current. Once you've done that, you can have a type with a dependency on ICacheManager and everything else will just work.

类似这样的东西:

在XML配置中:

<unity>
  <namespace name="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity" />
  <assembly name="Microsoft.Practices.EnterpriseLibrary.Common" />

  <container>
    <extension type="EnterpriseLibraryCoreExtension" />

    <register type="IMyType" mapTo="MyImplementation">
      <constructor>
        <param name="cacheManager" />
      </constructor>
    </register>
  </container>
</unity>

(您当然还将需要您的entlib配置).

(You will of course need your entlib configuration as well).

然后在应用程序启动时,输入如下代码:

Then in the startup of your application, have code like this:

var container = new UnityContainer()
    .LoadConfiguration();

EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

从那里一切都应该工作. EnterpriseLibraryContainer.Current将从您的容器中获取对象,而从容器中解析的对象将与其他任何依赖项一样注入entlib对象.

From there everything should just work. EnterpriseLibraryContainer.Current will get objects from your container, and objects resolved from the container will get entlib objects injected like any other dependency.

完成此步骤后,下一步是通过DI获取所有entlib对象,并删除对EnterpriseLibraryContainer.Current的显式调用.

Once you've gone this step, the next one is to get all your entlib objects via DI and drop the explicit calls to EnterpriseLibraryContainer.Current.

这篇关于我应该使用Unity Config文件还是Code来注册类型和实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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