我必须在所有类上实现 IDisposable,还是基类就足够了?

发布于 2024-12-15 12:55:36 字数 1525 浏览 4 评论 0原文

有人告诉我需要 处理我的实体框架存储库类的实例,并且我创建了一个基类来强制执行此实现。

我需要咨询专家:通过基类实现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东北女汉子 2024-12-22 12:55:36

答案是“这取决于”。

如果某些超类中的“Dispose()”方法就足够了,那么您当然不需要在每个子类中重新实现它。

“超类”可能是基类;它可能是一个或多个子类。

这取决于您分配的内容以及需要清理的内容。

恕我直言...

这是 MSDN 所说的:

http://msdn.microsoft.com/en-us/magazine/ cc163392.aspx

当您从一次性类型派生时,并且该派生类型不
引入任何新资源,那么不需要做任何特别的事情。
基本类型 IDisposable 实现将负责清理工作
它的资源和你的子类可以幸福地忽略
细节。

<= 换句话说,您不必一遍又一遍地重新实现 Dispose

...但是...

但是,有一个包含 new 的子类也很常见
需要清理的资源。在这种情况下,您的班级需要
释放其资源,同时确保基类型的
资源也被释放。通过重写清理方法来做到这一点,
释放你的资源,然后调用基类型来清理
其资源,如图6所示。

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:

http://msdn.microsoft.com/en-us/magazine/cc163392.aspx

When you derive from a disposable type, and that derived type does not
introduce any new resources, then nothing special needs to be done.
The base type IDisposable implementation will take care of cleaning up
its resources and your subclass can be blissfully ignorant of the
details.

<= In other words, you DON'T necessarily have to re-implement Dispose over and over

... but ...

However, it’s also common to have a subclass that does contain new
resources that need to be cleaned up. In this case, your class needs
to release its resources, while making sure that the base type’s
resources also get released. Do this by overriding the cleanup method,
releasing your resources, and then calling the base type to clean up
its resources, as in Figure 6.

梦在深巷 2024-12-22 12:55:36

这取决于派生类中是否有需要释放的资源。如果存在特定于派生类的资源,仅在基类上实现 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.

玩套路吗 2024-12-22 12:55:36

一般来说,您必须在每个类中实现 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.

这样的小城市 2024-12-22 12:55:36

使用基类进行处理就可以了。这里重要的是清理非托管资源,在本例中这意味着关闭数据库连接。我认为你最好使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文