Entity framework 统一';s PerResolveLifetimeManager,用于存储库、工作单元和实体框架DBContext

Entity framework 统一';s PerResolveLifetimeManager,用于存储库、工作单元和实体框架DBContext,entity-framework,.net-3.5,unity-container,repository-pattern,unit-of-work,Entity Framework,.net 3.5,Unity Container,Repository Pattern,Unit Of Work,我想将存储库、服务和UoW注入应用层,并将DBcontext注入UoW和存储库 DBContext在UoW和AppLayer中的每个存储库中必须是相同的上下文,但必须在AppLayer被释放后被释放,并且必须在每个AppLayer解析中创建新的DBContext Unity的DBContext类型映射配置中的PerResolveLifetimeManager是否适合这种情况 例如: //main appLayer = resolve<IAppLayer> appLayer.doSom

我想将存储库、服务和UoW注入应用层,并将DBcontext注入UoW和存储库

DBContext在UoW和AppLayer中的每个存储库中必须是相同的上下文,但必须在AppLayer被释放后被释放,并且必须在每个AppLayer解析中创建新的DBContext

Unity的DBContext类型映射配置中的PerResolveLifetimeManager是否适合这种情况

例如:

//main
appLayer = resolve<IAppLayer>
appLayer.doSomeStuff()
appLayer.dispose()
// end main

//applayer class
public class AppLayer : IAppLayer{

  AppLayer(IRepository, IBusinesService, IUoW){...//init vbles} //ctor, dependencies injected by Unity

  public void doSomeStuff(){

    using(transactionScope){

      businessEntity = IRepository.findEntity()
      IBusinessService.modifyEntity(businessEntity) 
      IUoW.saveChanges() //works because IRepository is using the same DBContext to find the entity, so the entity is attached to the same DBContext.

    }//end using

  }//end doSomeStuff

}//end applayerclass
//main
appLayer=解析
appLayer.doSomeStuff()
appLayer.dispose()
//端干管
//applayer类
公共类应用层:IAppLayer{
AppLayer(IRepository,IBusinesService,IUoW){…//init vbles}//ctor,Unity注入的依赖项
公共空间doSomeStuff(){
使用(transactionScope){
businessEntity=IRepository.findEntity()
IBusinessService.modifyEntity(businessEntity)
IUoW.saveChanges()//之所以有效,是因为IRepository使用相同的DBContext查找实体,因此该实体附加到相同的DBContext。
}//终端使用
}//末日
}//端苹果叶丛

PerResolveLifetimeManager非常适合此场景。我做了一个实证测试,检查持久性缓存和实例哈希代码。

这个答案让我很高兴。