Domain driven design 是否可以将Datacontext设置为存储库中的属性?

Domain driven design 是否可以将Datacontext设置为存储库中的属性?,domain-driven-design,repository-pattern,datacontext,Domain Driven Design,Repository Pattern,Datacontext,将datacontext设置为如下属性时是否存在任何潜在问题: 存储库 public Repository() { public DataContext dc {get;set;} public GetOrders(int id) { ...from dc.Orders...} } 服务层: public GetNewOrders() { .... Repository rep=new Repository();

将datacontext设置为如下属性时是否存在任何潜在问题:

存储库

public Repository()
{ 
    public DataContext dc {get;set;}

    public GetOrders(int id)
       { ...from dc.Orders...}
}
服务层:

public GetNewOrders()
   {
       ....
       Repository rep=new Repository();
       using {DataContext dc=new DataContext())
        { 
           rep.dc=dc;
           rep.GetOrders(id);
        }
    }

在DDD中,通过引用concret类,您错过了这里的大图。根据最佳实践,您没有在存储库和“服务层”之间进行接口。如果必须将DataContext注入到存储库中,我建议重构:

public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  private IDataContext _dataContext;
  public Repository(IDataContext dataContext)
  {
    _dataContext = dataContext;
  }
  public IList<Orders> GetNewOrders()
  {
    // perform your actions on _dataContext here
  }
}
这样,您就可以完全控制DataContext实例在“服务”层之外的控制方式。因此,您可以使用此DataContextFactory,如下所示:

public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  public IList<Orders> GetNewOrders()
  {
    using (var dataContext = DataContextFactory.GetInstance())
    {
      // dataContext is now your IDataContext to work with
    }
  }
} 

如果您不熟悉服务定位器的概念,请查看Castle Windsor,因为它封装了您的所有需求

根据我所读的内容,使用DataContext“.”向下滚动到引用的为什么这很重要?部分。由于缓存和其他因素,您应该立即考虑您的DATACONTURN STATLE。因此,可以肯定地说,您不希望将DataContext保留为所有方法都重用的属性。使用Eric Duncan的建议,您将希望传入某种类型的DataContext工厂,以便为每个查询获取新的上下文


对于以DataContext为重点的讨论,APressPro LINQ一书对DataContext进行了详细介绍,其中还建议您“立即考虑DataContext过时。”

在何处以及如何处置DataContext?使用后立即处置它。换句话说,只在using块中使用它。由于DataContext实例对IRepository实例是私有的,因此当IRepository被分发时,IDisposal负责垃圾收集。因此,您不必处理它。如果您确实想控制它,请在上面的DataContextFactory中使用using()。同样,使用DataContextFactory“factory”模式,您可以使用InstanceOf模式控制该对象(即本例中的DataContext对象)的生活方式。保持周围有一个静态引用。仅对新线程等使用using()。您在该级别上处于控制状态。谢谢!投票赞成你的答案,因为这是一个很好的参考/链接。谢谢。我添加了更多的链接。但它位于服务层的using块中,因此在调用存储库后它会“过时”。如果你按字面意思理解引号,那么它在你使用它之前就过时了,所以你应该尽快使用并摆脱它。如果您像示例中那样保持using块良好且紧凑,“在调用存储库之后”,using块将结束,因此您应该是良好的。
public static class DataContextFactory
{
  public static IDataContext GetInstance()
  {
    // return either a static instance, 
    // or threaded instance, a GlobalContext instance
    // or whatever your preference is here
    // 
  }
}
public interface IRepository
{
  IList<Orders> GetNewOrders();
}
public Repository : IRepository
{
  public IList<Orders> GetNewOrders()
  {
    using (var dataContext = DataContextFactory.GetInstance())
    {
      // dataContext is now your IDataContext to work with
    }
  }
} 
public void GetNewOrdersForServices()
{
  // Not recommended!
  //      IRepository repo = new Repository()
  //
  // The following is recommended instead; because, it removes the
  // the Concret reference from your Services layer completely!
  //
  IRepository repo = ServiceLocator.InstanceOf<IRepository>();
  IList myList = repo.GetNewOrders();

}
public class OrderService
{
  private IRepository _repo;

  public OrderService(IRepository repo)
  {
    _repo = repo;
  }

  public void GetNewOrdersForServices()
  {
    IList myList = _repo.GetNewOrders();

  }