C# Unity子容器层次结构CallifeTimeManager mvc和windows服务

C# Unity子容器层次结构CallifeTimeManager mvc和windows服务,c#,asp.net-mvc,entity-framework,unity-container,object-lifetime,C#,Asp.net Mvc,Entity Framework,Unity Container,Object Lifetime,我在我的业务层级别上有以下类(简化代码): 因此,目前在MVC端,我使用PerRequestLifetimeManager来控制DbContext的生存期 但现在在windows服务中,我无法使用它,因此我打算执行以下操作,但感觉不对,因为它看起来很像ServiceLocator: using(var container = ConfiguredContainer.CreateChildContainer()) { var statusUpdater = container.Resolve

我在我的业务层级别上有以下类(简化代码):

因此,目前在MVC端,我使用PerRequestLifetimeManager来控制DbContext的生存期

但现在在windows服务中,我无法使用它,因此我打算执行以下操作,但感觉不对,因为它看起来很像ServiceLocator:

using(var container = ConfiguredContainer.CreateChildContainer())
{
   var statusUpdater = container.Resolve<IStatusUpdater>();
   statusUpdater.UpdateStatus("test");
}
使用(var container=ConfiguredContainer.CreateChildContainer())
{
var statusUpdater=container.Resolve();
statusUpdater.UpdateStatus(“测试”);
}
还有其他选择吗?有没有一种方法可以在MVC应用程序和windows服务中使用相同的代码,而不需要两种类型的注册:

MVC:

container.RegisterType(新的PerRequestLifetimeManager());
Windows服务:

container.RegisterType<IStatusRepo, StatusRepo>(new HierarchicalLifetimeManager());
container.RegisterType(新的层次结构CallifetimeManager());

我通常在它们自己的程序集中注册我的类型,很可能与您一样,但是当有特定于执行程序集的内容时,我会在该执行程序集的注册中重写它

// In BusinessProcessor
container.RegisterType<IBusinessProcessorA, MyBusinessProcessorA1>();
container.RegisterType<IBusinessProcessorA, MyBusinessProcessorA2>();
container.RegisterType<IBusinessProcessorB, MyBusinessProcessorB1>();

// In DataAccessLayer
container.RegisterType<IRepository, Repository<A>>("A", new HierarchicalLifetimeManager());
container.RegisterType<IRepository, Repository<B>>("B", new HierarchicalLifetimeManager());
container.RegisterType<IRepository, Repository<C>>("C", new HierarchicalLifetimeManager());

// In WindowsService
Register(BusinessProcessor);    // Call to have the BusinessProcessor register it's own things.
Register(DataAccessLayer);      // Call to have the DataAccessLayer register it's own things.
container.RegisterType<IService, MyService>();

// In WebApplication
Register(BusinessProcessor);    // Call to have the BusinessProcessor register it's own things.
Register(DataAccessLayer);      // Call to have the DataAccessLayer register it's own things.
container.RegisterType<IController, MyController>();
container.RegisterType<IRepository, Repository<A>>("A", new PerRequestLifetimeManager());
//在BusinessProcessor中
container.RegisterType();
container.RegisterType();
container.RegisterType();
//在数据访问层中
RegisterType(“A”,新的层次结构CallifetimeManager());
RegisterType(“B”,新的层次结构CallifetimeManager());
RegisterType(“C”,新的层次结构CallifetimeManager());
//在Windows服务中
注册(业务处理器);//打电话让BusinessProcessor注册它自己的东西。
寄存器(DataAccessLayer);//调用让DataAccessLayer注册它自己的东西。
container.RegisterType();
//在WebApplication中
注册(业务处理器);//打电话让BusinessProcessor注册它自己的东西。
寄存器(DataAccessLayer);//调用让DataAccessLayer注册它自己的东西。
container.RegisterType();
RegisterType(“A”,新的PerRequestLifetimeManager());

另一种方法是在DAL中使用不同的命名注册来注册存储库,并对BusinessProcessors进行注册,但这意味着您的整个解决方案都知道这一事实,我不推荐这样做。完全可以。

对Unity不太确定,但在其他DI框架中,您必须为
MVC
赢得服务使用不同的注册码。直接调用
Resolve
方法总是“嗅到”,因为这就是DI框架的目的——在需要时解析实例。当我们在代码中调用
.Resolve
方法时,这意味着我们很可能做错了什么。我们可以在构造函数中注入实例,并更改使用它们的对象的生命范围。
container.RegisterType<IStatusRepo, StatusRepo>(new HierarchicalLifetimeManager());
// In BusinessProcessor
container.RegisterType<IBusinessProcessorA, MyBusinessProcessorA1>();
container.RegisterType<IBusinessProcessorA, MyBusinessProcessorA2>();
container.RegisterType<IBusinessProcessorB, MyBusinessProcessorB1>();

// In DataAccessLayer
container.RegisterType<IRepository, Repository<A>>("A", new HierarchicalLifetimeManager());
container.RegisterType<IRepository, Repository<B>>("B", new HierarchicalLifetimeManager());
container.RegisterType<IRepository, Repository<C>>("C", new HierarchicalLifetimeManager());

// In WindowsService
Register(BusinessProcessor);    // Call to have the BusinessProcessor register it's own things.
Register(DataAccessLayer);      // Call to have the DataAccessLayer register it's own things.
container.RegisterType<IService, MyService>();

// In WebApplication
Register(BusinessProcessor);    // Call to have the BusinessProcessor register it's own things.
Register(DataAccessLayer);      // Call to have the DataAccessLayer register it's own things.
container.RegisterType<IController, MyController>();
container.RegisterType<IRepository, Repository<A>>("A", new PerRequestLifetimeManager());