我必须在所有类上实现 IDisposable,还是基类就足够了?
有人告诉我需要 处理我的实体框架存储库类的实例,并且我创建了一个基类来强制执行此实现。
我需要咨询专家:通过基类实现 IDisposable 是否可以接受?
请注意,存储库类没有类成员变量。
/// Sample repository. Note that I return List<T> as IEnumerable,
/// and I use IDisposable
///
public class CompanyRepository : DisposableBase, ICompanyRepository
{
public IEnumerable<CompanyDetail> GetOneCompany(int? CompanyID)
{
var t = from c in _entities.CompanyDetail
where c.CompanyID == CompanyID.Value
select c;
return t.ToList();
}
}
/// <summary>
/// Disposable implementation based on advice from this link:
/// from Http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
/// </summary>
public class DisposableBase : IDisposable
{
protected TLSAdminEntities1 _entities;
public DisposableBase()
{
_entities = new TLSAdminEntities1();
disposed = false;
}
private bool disposed ;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_entities.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
I am told I need to dispose of instances of my Entity Framework repository classes and I created a base class to enforce this implementation.
I need to check with the experts: is it acceptable to implement IDisposable
through a base class?
Note that the repository class has no class member variables.
/// Sample repository. Note that I return List<T> as IEnumerable,
/// and I use IDisposable
///
public class CompanyRepository : DisposableBase, ICompanyRepository
{
public IEnumerable<CompanyDetail> GetOneCompany(int? CompanyID)
{
var t = from c in _entities.CompanyDetail
where c.CompanyID == CompanyID.Value
select c;
return t.ToList();
}
}
/// <summary>
/// Disposable implementation based on advice from this link:
/// from Http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
/// </summary>
public class DisposableBase : IDisposable
{
protected TLSAdminEntities1 _entities;
public DisposableBase()
{
_entities = new TLSAdminEntities1();
disposed = false;
}
private bool disposed ;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_entities.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答案是“这取决于”。
如果某些超类中的“Dispose()”方法就足够了,那么您当然不需要在每个子类中重新实现它。
“超类”可能是基类;它可能是一个或多个子类。
这取决于您分配的内容以及需要清理的内容。
恕我直言...
这是 MSDN 所说的:
<= 换句话说,您不必一遍又一遍地重新实现 Dispose
...但是...
The answer is "it depends".
If the "Dispose()" method in some superclass is sufficient, you certainly don't need to reimplement it in each and every subclass.
The "superclass" might be the base class; it might be one or more subclasses.
It depends on what you allocate, and what needs to be cleaned up.
IMHO...
Here is what MSDN has to say:
<= In other words, you DON'T necessarily have to re-implement Dispose over and over
... but ...
这取决于派生类中是否有需要释放的资源。如果存在特定于派生类的资源,仅在基类上实现 IDisposable 是不够的。
It depends on whether you have resources in the derived class that need to be disposed. If there are resources specific to the derived class, implementing
IDisposable
only on the base class will not be sufficient.一般来说,您必须在每个类中实现 IDispoable,其中您有自己实现 IDisposable 的私有成员。这些资源必须被“释放”。我强烈建议您阅读 CodeProject 上关于 IDisposable 模式的这篇非常好的论文。
Generaly spoken, you have to implement IDispoable in every class, where you have private members which themselves implement IDisposable. Those resources have to be "freed". I strongly advise you to read this very good paper on CodeProject about the IDisposable pattern.
使用基类进行处理就可以了。这里重要的是清理非托管资源,在本例中这意味着关闭数据库连接。我认为你最好使用 httpmodules 或操作过滤器之类的东西连接到 asp.net 来处理你的工作单元并为每个请求的工作单元类型的设置进行处理,但是如果相反,您只需确保在存储库实例上调用 dispose,拥有一个处理实体框架上下文的基类就可以了(即使使用过滤器/模块处理它们,您仍然可以使用这些存储库的基类)。
it is fine to dispose using a base class. the important thing here is cleaning up unmanaged resources, which in this case means closing database connections. i'd argue that you'd be better off hooking into asp.net with things like httpmodules or action filters to handle your unit of work and do the dispose for a unit-of-work-per-request type of setup, but if you are instead just making sure to call dispose on your repository instances, having a base class that disposes entity framework context is fine (and you could still use the base class for these repositories even with disposing them with a filter/module).