How do I register components at run time with unity DI

萝らか妹 提交于 2021-01-29 08:23:13

问题


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?

Note: I'm using Code First.

All assistance will be appreciated.


回答1:


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" />

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>

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;
    }
}

That's all there is to it.



来源:https://stackoverflow.com/questions/16994089/how-do-i-register-components-at-run-time-with-unity-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!