【问题标题】:Unity's PerResolveLifetimeManager for Repositories, Unit of Work and Entity Framework DBContextUnity 的 PerResolveLifetimeManager 用于存储库、工作单元和实体框架 DBContext
【发布时间】:2013-01-04 13:36:27
【问题描述】:

我想将存储库、服务和 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

【问题讨论】:

    标签: entity-framework .net-3.5 unity-container repository-pattern unit-of-work


    【解决方案1】:

    PerResolveLifetimeManager 非常适合这种情况。我做了一个经验测试,检查持久性缓存和实例哈希码。

    【讨论】:

    • 这个答案让我很开心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多