【发布时间】: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()
{}
}
- 我想知道每次调用的 DbContext 范围在我的场景中是否有效?
- 在我的服务方法中创建存储库的方法是否有效?
- 是否有任何最佳做法可以在不使用 IOC 的情况下将其连接起来?
【问题讨论】:
标签: c# wcf entity-framework dbcontext