Вопрос

We are implementing ntier architecture for one of the project which uses EF6 ORM. DbContext scope is managed by ContextStoreFactory. Based on configuration ContextStoreFactory uses HttpContextStore/StaticContextStore to create DbContext. For console app its working fine. Now we planning for implemnting a wcf service with net.msmq binding which uses underneath services to process incoming request.

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);
  }
}

I would like to manage the DbContext scope in wcf service. I come across many articles which says its best to use DBContext per call/operation. My sample OperationContextStore looks like follows. Please feel free to correct if it requires any correction.

 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. I would like to know Is DbContext scope per call is valid in my scenario?
  2. Is the approach to create Repository in my service method is valid?
  3. Are there any best practices to wire this up without using IOC?
Это было полезно?

Решение

I know it's late to answer my own question and I will try to recollect what I did

  1. I would like to know Is DbContext scope per call is valid in my scenario?

    Yes,It was valid in my scenario.

  2. Is the approach to create Repository in my service method is valid?

    I ended up having IRepository as a property in my service class and did a property injection.

  3. Are there any best practices to wire this up without using IOC?

    I ended up writing my own utility. Please search for poor man's dependency injection.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top