【问题标题】:Managing DbContext scope in wcf service without IOC?在没有 IOC 的情况下管理 wcf 服务中的 DbContext 范围?
【发布时间】:2014-02-21 06:29:01
【问题描述】:

我们正在为使用 EF6 ORM 的项目之一实施 ntier 架构。 DbContext 范围由 ContextStoreFactory 管理。基于配置 ContextStoreFactory 使用 HttpContextStore/StaticContextStore 来创建 DbContext。对于控制台应用程序,它工作正常。现在我们计划用 net.msmq 绑定实现一个 wcf 服务,它使用底层服务来处理传入的请求。

public class TestService : ITestService
{
  public void ProcessPerson(Person person)
  {
     var repo = GetRepository();
     var personService = new PersonService(repo);
     personService.Process(person);
  }

  private IRepository GetRepository()
  {
    var context = ContextStoreFactory.GetContextStore().GetContext();//Calls OperationcontextStore
    return new Repository(context);
  }
}

我想在 wcf 服务中管理 DbContext 范围。我遇到很多文章说最好每次调用/操作使用 DBContext。我的示例 OperationContextStore 如下所示。如果需要更正,请随时更正。

 public class OperationContextStore
 {
    public static readonly string ITEM_NAME = "DBCONTEXT-INSTANCES";

    public DBContext GetContext()
    {
      if(!OperationContext.Current.OutgoingMessageProperties.ContainsKey(ITEM_NAME))
          OperationContext.Current.OutgoingMessageProperties.Add(ITEM_NAME, new  DBContext());
      return (DBContext)OperationContext.Current.OutgoingMessageProperties[ITEM_NAME]; 
    }

    public void Dispose()
    {}
 }
  1. 我想知道每次调用的 DbContext 范围在我的场景中是否有效?
  2. 在我的服务方法中创建存储库的方法是否有效?
  3. 是否有任何最佳做法可以在不使用 IOC 的情况下将其连接起来?

【问题讨论】:

    标签: c# wcf entity-framework dbcontext


    【解决方案1】:

    我知道现在回答我自己的问题已经很晚了,我会努力回忆我的所作所为

    1. 我想知道每次调用的 DbContext 范围在我的场景中是否有效?

      是的,它在我的场景中有效。

    2. 在我的服务方法中创建存储库的方法是否有效?

      我最终将 IRepository 作为我的服务类中的一个属性并进行了属性注入。

    3. 是否有任何最佳做法可以在不使用 IOC 的情况下将其连接起来?

      我最终编写了自己的实用程序。请搜索穷人的依赖注入。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2012-10-22
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-19
      • 2012-04-20
      • 1970-01-01
      相关资源
      最近更新 更多