将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);
}
}
最佳答案
根据我的阅读,使用 DataContext “for more than one business conversation is usually the wrong thing to do 。”向下滚动到引用的为什么如此重要?部分。由于缓存和其他因素,您应该立即考虑您的 DataContext 陈旧。由此可以肯定地说,您不想将 DataContext 保留为所有方法都重用的属性。使用 Eric Duncan 的建议,您需要传入某种 DataContext 工厂来为每个查询获取新的上下文。
关于 DataContext 的讨论,APress Pro LINQ 一书中有 entire chapter在 DataContext 上,very last page其中还建议您“立即考虑 DataContext 陈旧。”
关于domain-driven-design - 可以将 Datacontext 设置为存储库中的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/615960/