Csharp/C#教程:我可以像在Autofac中那样在Unity中的模块中注册我的类型吗?分享


我可以像在Autofac中那样在Unity中的模块中注册我的类型吗?

我对Autofac非常熟悉,我非常喜欢Autofac的一个function是注册模块。 有谁知道我怎么能用Unity做到这一点? 如果有的话,我很难找到在Google中使用哪些术语来提出等效的统一。

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

你不能。 只需使用Autofac或Windsor。 您会发现Unity中缺少很多东西 ,并且它们以意想不到的方式运行。 这不值得你花时间。

实际上,您可以使用Unity容器扩展轻松完成。

上述就是C#学习教程:我可以像在Autofac中那样在Unity中的模块中注册我的类型吗?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 public class Global : HttpApplication, IContainerProviderAccessor { private static IContainerProvider _containerProvider; protected void Application_Start(object sender, EventArgs e) { var container = new UnityContainer(); container.AddNewExtension(); _containerProvider = new ContainerProvider(container); } [...] public IContainerProvider ContainerProvider { get { return _containerProvider; } } } public class MyWebModule : UnityContainerExtension { protected override void Initialize() { Container.AddNewExtension(); Container.AddNewExtension(); } } public class ApplicationModule: UnityContainerExtension { protected override void Initialize() { Container.RegisterType( new ContainerControlledLifetimeManager(), new InjectionFactory(c => new ProductPresenter(c.Resolve()))); } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/941995.html

(0)
上一篇 2021年11月12日
下一篇 2021年11月12日

精彩推荐