makerofthings7
makerofthings7

Reputation: 61473

Must I implement IDisposable on all classes, or is a base class sufficient?

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);
    }
}

Upvotes: 7

Views: 5773

Answers (4)

James Johnson
James Johnson

Reputation: 46067

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.

Upvotes: 4

Dave Rael
Dave Rael

Reputation: 1759

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).

Upvotes: 0

Fischermaen
Fischermaen

Reputation: 12468

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.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121819

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.

Upvotes: 7

Related Questions