【发布时间】:2023-03-13 12:08:01
【问题描述】:
我在 Internet 上找到了一篇介绍如何实现存储库模式的文章。实现看起来类似于这里:
class ProductRepository : IProductRepository
{
var context;
public ProductRepository() {
this.context = new MyDataBaseDataContext();
}
// the rest of methods
}
但我不太确定这是否正确,上下文发生了什么?垃圾收集器是否处理此对象?或者我应该更好地使用 using (...) { } 语句创建上下文?
【问题讨论】:
-
这对我来说似乎完全有效。如果您的存储库类拥有一次性数据上下文,它本身也应该是一次性的(符合任何具有一次性成员的类也应该实现 IDisposable 的准则)。
标签: c# linq-to-sql repository-pattern datacontext