2

Reading up alot on how to implement the Dispose pattern in a typical layered MVC architecture, I have come across alot of answers and even contradictions, which has now confused me.

I'm using the Domain Model pattern, and Repository pattern. Note, not using UoW, or Dependency Injection. I may use DI soon (not sure yet), which means the architecture would change and the Dispose technique also.

Anyway, here is the code.

Controller

    protected override void Dispose(bool disposing)
    {
        _myService.Dispose();

        // Call base class implementation
        base.Dispose(disposing);
    }

Service

    public void Dispose()
    {
        _myRepository.Dispose();
    }

Domain

public interface IMyRepository : IDisposable

Repository

Extra question: Is the IDisposable required here?

    public class MyRepository : IMyRepository, IDisposable


    // Flag, has Dispose already been called?
    private bool disposed = false;

    // Protected implementation of Dispose pattern
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                // Free any other managed objects here
                _context.Dispose();
            }
        }

        // Free any unmanaged objects here
        this.disposed = true;
    }

    // Public implementation of Dispose pattern callable by consumers
    public void Dispose()
    {
        // Dispose of unmanaged resources
        Dispose(true);
        // Suppress finalization
        GC.SuppressFinalize(this);
    }

I have put together (below) what I believe is correct, but not entirely sure.

Question. Will the above code cleanup all unmanaged resources, e.g. database connections? (i.e. is what I've done correct)

3
  • You asked a few related questions, I would recommend editing and putting the question you are most interested in getting an answer about as the last line, and in bold, and then related questions in non-bold. Otherwise answers may choose the easiest answer and ignore others. I would image the most important one being "Will this cleanup all unmanaged resources (database connections, etc.)?" Commented Jul 15, 2014 at 1:05
  • Thanks for the tip, updated it :) Commented Jul 15, 2014 at 1:15
  • I've just found this by chance while looking into something else, both the repository class and it's interface class have the IDisposable included. This is by a Microsoft web guy: asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/… Commented Jul 15, 2014 at 1:57

1 Answer 1

1

no, not on MyRepository as you've already inherited it though IMyRepository

Sign up to request clarification or add additional context in comments.

1 Comment

Was unsure about this, i.e. implementing an interface doesn't just bind you to a contract to implement the methods, properties, events... in the concrete class, but also does inheritance. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.