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?
You can just use…
…without virtual
Disposeoverload and without privatedisposedflag becauseDisposehas already been called so that nothing happens on a second call and no exception will be thrownDisposeexplicitlyThe 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 ausingblock.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().