Search code examples
c#asp.net-mvcentity-frameworkdisposeunit-of-work

Why does need implement Dispose pattern on UnitOfWork EF?


The microsoft tutorial http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application advises to implement dispose pattern, like this:

private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

Why i should do it, why i cannot simply dispose context and enough, what happens if i will use only:

context.Dispose()

Which goals of implementation of Microsoft's dispose pattern?


Solution

  • You can just use...

        public void Dispose() // IDisposable implementation
        {
            context.Dispose();
        }
    

    ...without virtual Dispose overload and without private disposed flag because

    • the context itself checks if Dispose has already been called so that nothing happens on a second call and no exception will be thrown
    • the context class has its own finalizer that will ensure that the database connection is released on garbage collection if you didn't call Dispose explicitly

    The last point doesn't mean btw that you don't need to call context.Dispose() at all because the point in time when the garbage collector will finalize the context is indeterministic and it might be later than the point when you have created a new context instance and use it possibly with the same entities - which can cause some trouble. So: Always dispose a context explicitly or by a using block.

    I also have doubt that GC.SuppressFinalize(this); has any effect here because you don't have a finalizer in your class nor in a base class, so there is nothing to suppress.

    In my opinion the pattern in your example is a fragment (that misses a finalizer/destructor implementation) and that might be useful if you have to deal with your own unmanaged resources in your class. But for your UnitOfWork class you don't have to. The unmanaged resource (database connection) is managed by the context and you only need to delegate the work to it by calling context.Dispose().