Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I register my types in modules in Unity like I can in Autofac?

I am fairly familiar with Autofac and one feature that I really love about Autofac is the registering of modules. Does anyone know how I can do this with Unity? I'm having a hard time finding which terms to use in Google to come up with the unity equivalent if there is one.


public class Global : HttpApplication, IContainerProviderAccessor
{
   private static IContainerProvider _containerProvider;

   protected void Application_Start(object sender, EventArgs e)
   {
      var builder = new ContainerBuilder();
      builder.RegisterModule(new MyWebModule());

      _containerProvider = new ContainerProvider(builder.Build());
   }

[...]

   public IContainerProvider ContainerProvider
   {
      get { return _containerProvider; }
   }
}

public class MyWebModule: Module
{
    protected override void Load(ContainerBuilder builder)
    {
       builder.RegisterModule(new ApplicationModule());
       builder.RegisterModule(new DomainModule());
    }
}

public class ApplicationModule: Module
{
    protected override void Load(ContainerBuilder builder)
    {
       builder.Register(c => new ProductPresenter(c.Resolve<IProductView>()))
                .As<ProductPresenter>()
                .ContainerScoped();
    }
}

like image 377
Adam Avatar asked Aug 06 '10 20:08

Adam


People also ask

Why do we need AutoFac?

AutoFac provides better integration for the ASP.NET MVC framework and is developed using Google code. AutoFac manages the dependencies of classes so that the application may be easy to change when it is scaled up in size and complexity.

What is AutoFac dependency injection?

Autofac is an open-source dependency injection (DI) or inversion of control (IoC) container developed on Google Code. Autofac differs from many related technologies in that it sticks as close to bare-metal C# programming as possible.

What is IoC unity?

Unity Inversion of Control (Unity IoC) Inversion of Control (IoC) is a concept stating that aggregated system modules should not depend on implementation of subordinate modules. Instead, a separate module-container stores abstraction-implementation couple and manages the lifetime of these implementations.


1 Answers

Actually, you can do trivially with Unity container extensions.

public class Global : HttpApplication, IContainerProviderAccessor
{
   private static IContainerProvider _containerProvider;

   protected void Application_Start(object sender, EventArgs e)
   {
      var container = new UnityContainer();
      container.AddNewExtension<MyWebModule>();

      _containerProvider = new ContainerProvider(container);
   }

[...]

   public IContainerProvider ContainerProvider
   {
      get { return _containerProvider; }
   }
}

public class MyWebModule : UnityContainerExtension
{
    protected override void Initialize()
    {
        Container.AddNewExtension<ApplicationModule>();
        Container.AddNewExtension<DomainModule>();
    }
}

public class ApplicationModule: UnityContainerExtension
{
    protected override void Initialize()
    {
        Container.RegisterType<ProductPrensenter>(
            new ContainerControlledLifetimeManager(),
            new InjectionFactory(c => new ProductPresenter(c.Resolve<IProductView>())));
    }
}
like image 60
Chris Tavares Avatar answered Sep 30 '22 04:09

Chris Tavares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!