gpt4 book ai didi

c# - 在没有 IOC 的情况下管理 wcf 服务中的 DbContext 范围?

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:51 24 4
gpt4 key购买 nike

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

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 per call/operation。我的示例 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. 在我的服务方法中创建 Repository 的方法是否有效?
  3. 是否有任何最佳做法可以在不使用 IOC 的情况下进行连接?

最佳答案

我知道现在回答我自己的问题已经晚了,我会努力记忆我做了什么

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

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

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

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

  3. 是否有任何最佳实践可以在不使用 IOC 的情况下进行连接?

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

关于c# - 在没有 IOC 的情况下管理 wcf 服务中的 DbContext 范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21926902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com