我相当熟悉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.