Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.Net MVC 3 Unit of Work Disposal
    primarykey
    data
    text
    <p>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 <a href="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" rel="nofollow">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</a></p> <p>However, instead of injecting my UoW class into my Controller, I do so in my Service Class, like so</p> <pre><code>public class ListService : IListService { private IUnitOfWork _uow; public ListService(IUnitOfWork uow) { _uow = uow; } public IList&lt;List&gt; GetAllListTypes() { return _uow.Lists.Get().OrderBy(l =&gt; l.description).ToList(); } } </code></pre> <p>My Unit of Work class is like this</p> <pre><code>public class UnitOfWork : IUnitOfWork, IDisposable { readonly LocumEntities _context = new LocumEntities(); private GenericRepository&lt;List&gt; _lists = null; public IGenericRepository&lt;List&gt; Lists { get { if (_lists == null) { _lists = new GenericRepository&lt;List&gt;(_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); } } </code></pre> <p>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</p> <pre><code>protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } </code></pre> <p>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</p> <pre><code>protected override void Dispose(bool disposing) { _uow.Dispose(); } </code></pre> <p>I would greatly appreciate if someone could point me in the correct direction with how to dispose of my UoW class.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload