我想将存储库、服务和 UoW 注入应用程序层,并将 DBcontext 注入 UoW 和存储库。
DBContext 在 UoW 和 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