Note that there are some explanatory texts on larger screens.

plurals
  1. POProper way of using Unit of Work with unity injection
    primarykey
    data
    text
    <p>I am using unity, entity framework 4 with POCO classes, repository pattern for DAL and services for Business Logic control. I also want to use Unit of Work so I can package together CRUD operations which I perform on different services and then commit them all together.</p> <p>My question is what would be the proper way to inject the Unit Of Work mechanism into my application using Microsoft Unity? I understand that I can put the IUnitOfWork together with the repository on the constructor of the proper service and then if Unity mapping is specified it would auto initiate the proper instances, but this way I do not pass the global unit of work but rather create a new instance on each level, which can't be a smart way to do it (actually the repository is initiated even before the service).</p> <p>What am I missing? (Attached is constructor code as I wrote it now of service and its repository).</p> <p>U also understand that I can use Unity's ParameterOverrides method to take some global instance of Unit of Work (lets say from my aspx.cs file) and pass it into the service and then into the repository. But it seems a bit lame. Is this my only option?</p> <p>Thanks</p> <pre><code>public class GenericRepository&lt;T&gt; : IUnitOfWorkRepository, IGenericRepository&lt;T&gt; where T : BaseEntity, IAggregateRoot { private IUnitOfWork _uow; /// &lt;summary&gt; /// Returns the active object context /// &lt;/summary&gt; private ObjectContext ObjectContext { get { return ObjectContextManager.GetObjectContext(); } } public GenericRepository(IUnitOfWork uow) { _uow = uow; } //blahhhh... public void Add(T entity) { _uow.RegisterNew(entity, this); } public void Delete(T entity) { _uow.RegisterRemoved(entity, this); } //.....blah blah.... public void PersistCreationOf(IAggregateRoot entity) { this.ObjectContext.AddObject(GetEntitySetName(), entity); } public void PersistUpdateOf(IAggregateRoot entity) { // Do nothing as EF tracks changes } public void PersistDeletionOf(IAggregateRoot entity) { this.ObjectContext.DeleteObject(entity); } } public class CategoryRepository : GenericRepository&lt;XComSolutions.FB.Domain.Model.Entities.Category&gt;, ICategoryRepository { public CategoryRepository(IUnitOfWork uow) : base(uow) { } } public class CategoryService : ICategoryService { public int myID {get; set;} private ICategoryRepository _categoryRepository; private IUnitOfWork _uow; public CategoryService(ICategoryRepository categoryRepository, IUnitOfWork uow) { _categoryRepository = categoryRepository; _uow = uow; } public List&lt;Category&gt; GetAll() { return _categoryRepository.GetAll(); } } </code></pre>
    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.
 

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