如何在运行时使用unity DI注册组件 [英] How do I register components at run time with unity DI
问题描述
我创建了一个Microsoft统一项目(我第一次使用Dependency Injection),并且我的家庭控制器按预期进行了正常工作,并按如下所示注册了我的消息传递类
I created an Microsoft unity project (my first using Dependency Injection) with my home controller working fine as expected having registered my messaging Class as follows
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<IMessageService, MessageService>(new HierarchicalLifetimeManager());
RegisterTypes(container);
return container;
}
挑战在于,我打算使这个项目成为多个模块的父级,这些模块将成为插件,我该如何注册数据库连接性和操作方式?如何在不重新编译项目的情况下插入最初未添加的其他模块?
The challenge is I intend making this project a parent to several modules that will be plugins, how do I register my database connectivity and manipulations? How do I plugin other modules not added initially without recompiling the project?
注意:我使用的是Code First.
Note: I'm using Code First.
所有帮助将不胜感激.
推荐答案
首先,您需要注册Unity配置部分.看起来像这样:
First of all you need to register Unity configuration section. That looks like this:
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.1.505.0, Culture=neutral, PublickKeyToken=31bf3856ad364e35" />
这是Unity 2的配置.接下来是添加统一部分.
This is Unity 2 configuration. Next is to add unity section.
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<!-- General aliases -->
<alias alias="string" type="System.String, mscorlib" />
<alias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity, Version=2.1.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<!-- Interface aliases -->
<alias alias="ISampleService" type="MyApp.Api.Interfaces.Services.ISampleService, MyApp.Api" />
<alias alias="IMessagingService" type="MyApp.Api.Interfaces.Services.IMessagingService, MyApp.Api" />
<!-- Concrete implementations aliases -->
<alias alias="SampleServiceImpl" type="MyApp.BizLayer.SampleService, MyApp.BizLayer" />
<alias alias="MessagingServiceImpl" type="MyApp.BizLayer.SampleService, MyApp.BizLayer" />
<container>
<register type="SampleServiceImpl" mapTo="ISampleService"/>
<register type="MessagingServiceImpl" mapTo="IMessagingService">
<lifetime type="singleton" />
</register>
</container>
</unity>
在您的代码中,在global.asax中(如果是通过NuGet下载的,则在Unity Bootstrapper.cs中)中将使用以下代码:
In your code, in global.asax (or Unity Bootstrapper.cs if you downloaded it via NuGet) you would use something like this:
public static class UnityBootstrapper
{
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
var resolver = DependencyResolver.Current;
var newResolver = new Infrastructure.IoC.UnityDependencyResolver(container, resolver);
DependencyResolver.SetResolver(newResolver);
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
var unityConfigSection = WebConfigurationManager.GetSection("unity") as UnityConfigurationSection;
if (unityConfigSection != null)
{
unityConfigSection.Configure(container);
}
return container;
}
}
仅此而已.
这篇关于如何在运行时使用unity DI注册组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!