我可以注册我的类型在统一模块,如我可以在Autofac? [英] Can I register my types in modules in Unity like I can in Autofac?

查看:95
本文介绍了我可以注册我的类型在统一模块,如我可以在Autofac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相当熟悉Autofac和一个功能,我真的很喜欢Autofac是模块的登记。有谁知道我可以使用Unity做到这一点?我有一个很难找到在谷歌使用哪个方面拿出的统一等价的,如果有一个。


公共类全球:HttpApplication的,IContainerProviderAccessor
{
   私有静态IContainerProvider _containerProvider;

   保护无效的Application_Start(对象发件人,EventArgs的)
   {
      VAR建设者=新ContainerBuilder();
      builder.RegisterModule(新MyWebModule());

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

[...]

   公共IContainerProvider ContainerProvider
   {
      {返回_containerProvider; }
   }
}

公共类MyWebModule:模块
{
    保护覆盖无效负载(ContainerBuilder制造商)
    {
       builder.RegisterModule(新ApplicationModule());
       builder.RegisterModule(新DomainModule());
    }
}

公共类ApplicationModule:模块
{
    保护覆盖无效负载(ContainerBuilder制造商)
    {
       builder.Register(C =>新产品presenter(c.Resolve< IProductView>()))
                。随着<产品presenter>()
                .ContainerScoped();
    }
}

解决方案

您不能。只要使用Autofac或温莎。你会发现那里的很多在Unity失踪,那里有什么意想不到的方式工作。这只是不值得你的时间。

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

解决方案

You can't. Just use Autofac or Windsor. You will find there's a lot missing in Unity and what's there works in unexpected ways. It's just not worth your time.

这篇关于我可以注册我的类型在统一模块,如我可以在Autofac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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