Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere should I create the Unit of Work instance in an ASP.Net MVC 3 application?
    text
    copied!<p>I have read as many of the posts on Stackoverflow as I can find with regards the use of a Unit of Work pattern within an ASP.Net MVC 3 application which includes a Business Layer. However, I still have a couple of questions with regards this topic and would greatly appreciate any feedback people can give me.</p> <p>I am developing an ASP.Net MVC 3 Web application which uses EF 4.1. I will be using both the Repository and Unit of Work Patterns with this project similar to how they are used in <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="noreferrer">this</a> great tutorial </p> <p>The difference in my project is that I need to also include a Business Layer (separate project in my solution) in order to carry out the various business rules for the application. The tutorial mentioned above does not have a Business layer, and therefore creates an instance of the Unit of Work class from the controller</p> <pre><code>public class CourseController : Controller { private UnitOfWork unitOfWork = new UnitOfWork(); </code></pre> <p>However, my question is, where should I create the instance of the Unit of Work class if I have a Business Layer?</p> <p>I personally think it should be created in my controller and then injected into the Business Layer like so:</p> <pre><code>public class PeopleController : Controller { private readonly IUnitOfWork _UoW; private IPersonService _personService; public PeopleController() { _UoW = new UnitOfWork(); _personService = new PersonService(_UoW); } public PeopleController(IUnitOfWork UoW, IPersonService personService) { _UoW = UoW; _personService = personService; } public ActionResult Edit(int id) { Person person = _personService.Edit(id); return View(person); } public class UnitOfWork : IUnitOfWork, IDisposable { private BlogEntities _context = new BlogEntities(); private PersonRepository personRepository = null; public IPersonRepository PersonRepository { get { if (this.personRepository == null) { this.personRepository = new PersonRepository(_context); } return personRepository; } } public void Save() { _context.SaveChanges(); } public class PersonService : IPersonService { private readonly IUnitOfWork _UoW; public PersonService(IUnitOfWork UoW) { _UoW = UoW; } public Person Edit(int id) { Person person = _UoW.PersonRepository.GetPersonByID(id); return person; } public class PersonRepository : IPersonRepository { private readonly BlogEntities _context; public PersonRepository(BlogEntities context) { _context = context; } public Person GetPersonByID(int ID) { return _context.People.Where(p =&gt; p.ID == ID).Single(); } </code></pre> <p>I have read others saying that the Unit of Work instantiation should not be in the Controller, but created in the Service Layer instead. The reason why I am not so sure about this approach is because my Controller may have to use several different Service Layers in one business transaction, and if the Unit of Work instance was created inside each Service, it would result in several Unit of Work instances being created, which defeats the purpose, ie, one Unit of Work per business transaction.</p> <p>Maybe what I have explained above is wrong, but if so, I would greatly appreciate if someone could put me right.</p> <p>Thanks again for your help.</p>
 

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