簡體   English   中英

在沒有IOC的情況下在wcf服務中管理DbContext范圍?

[英]Managing DbContext scope in wcf service without IOC?

我們正在為使用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的情況下進行連接?

我知道回答我自己的問題已經太晚了,我將盡力回憶我所做的事情

  1. 我想知道每個調用的DbContext范圍在我的情況下是否有效?

    是的,在我的情況下這是有效的。

  2. 在我的服務方法中創建存儲庫的方法是否有效?

    我最終將IRepository作為服務類中的一個屬性,並進行了屬性注入。

  3. 是否有最佳實踐在不使用IOC的情況下進行連接?

    我最終編寫了自己的實用程序。 請搜索窮人的依賴注入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM