1

我希望将注入库,服务和UoW注入应用层并将DBcontext注入到UoW和注入存储库中。Unity的PerResolveLifetimeManager用于存储库,工作单元和实体框架DBContext

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 

回答

1

PerResolveLifetimeManager适合为这个完美的方案。我做了一个经验测试,检查持久性缓存和实例哈希代码。

+0

这个答案让我开心。 – Jeff

相关问题