1

I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My Unit of Work class is the same as described in this excellent 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

However, instead of injecting my UoW class into my Controller, I do so in my Service Class, like so

public class ListService : IListService
{
    private IUnitOfWork _uow;

    public ListService(IUnitOfWork uow)
    {
        _uow = uow;
    }

    public IList<List> GetAllListTypes()
    {
        return _uow.Lists.Get().OrderBy(l => l.description).ToList();
    }
}

My Unit of Work class is like this

public class UnitOfWork : IUnitOfWork, IDisposable
{
    readonly LocumEntities _context = new LocumEntities();
    private GenericRepository<List> _lists = null;

    public IGenericRepository<List> Lists
    {
        get
        {
            if (_lists == null)
            {
                _lists = new GenericRepository<List>(_context);
            }
            return _lists;
        }
    }

    public void Commit()
    {
        _context.SaveChanges();
    }

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

}

And this all works fine. However, you'll notice I have methods in my UoW class to dispose of the DbContext. I wish to dispose of the UoW class after every business transaction like is done in the above mentioned tutorial, they do this by using the following code in their controller

protected override void Dispose(bool disposing)
{
        db.Dispose();
        base.Dispose(disposing);
}

I tried to amend this method and place it in my service class however it does not work, ie, the dispose method in the UoW class never gets called

protected override void Dispose(bool disposing)
{
        _uow.Dispose();
}

I would greatly appreciate if someone could point me in the correct direction with how to dispose of my UoW class.

Thank you.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • What kind of service are you creating? Is it ASP.NET Web API, WCF, Windows service, business logic service or what? Is Dispose of your service ever called? Who is responsible for disposing your service? – Ladislav Mrnka Mar 28 '12 at 13:12
  • @LadislavMrnka - The Service class is an ASP.Net class setup for my business logic, it is in my service layer, which is between my UI's and my Domain Model. I don't have a dispose method in my service class. – tcode Mar 28 '12 at 13:37
  • @tgriffiths - what IoC container do you use for dependency injection? Most of them are capable of disposing components they create. – Sergey Rybalkin Mar 28 '12 at 13:46
  • @SergeyRybalkin - I use Unity for my IoC Container. – tcode Mar 28 '12 at 13:52
  • @tgriffiths - looks like it is possible to make Unity take care of your disposable components, check out this one - http://stackoverflow.com/questions/5129789/unity-2-0-and-handling-idisposable-types-especially-with-perthreadlifetimemanag. However I would suggest to switch to something like Autofac which plays nicely with deterministic disposal – Sergey Rybalkin Mar 28 '12 at 14:04
  • If you use Unity in combination with MVC 3 then possibly this is an option: http://unitymvc3.codeplex.com/ It takes care that the unit of work is disposed automatically after each request. I'm using it in a project with exactly the same structure: Inject service interfaces with UOW into controllers, and it works fine. – Slauma Mar 28 '12 at 15:33
  • Thanks for all your comments folks. – tcode Apr 02 '12 at 09:02

1 Answers1

0

Folks

The answer to this is, as I am using Unity for my IoC, it can be setup to take care of disposing of my Unit of Work class.

tcode
  • 5,055
  • 19
  • 65
  • 124
  • 3
    Can you please share how you achieved this - i.e. did you use a built-in Unity LifeTimeManager? From my understanding the default TransientLifetimeManager newes up a instance for each call and doesn't handle calling Dispose(). Basically, how did you register your UOW with Unity? – Mark Erasmus Oct 01 '13 at 10:15