Note that there are some explanatory texts on larger screens.

plurals
  1. POInteraction between unit of work and repository patterns
    primarykey
    data
    text
    <p>After reading thorugh plenty of articles I am still unsure about the responsibilities of Unit of Work pattern when interacting with repositories.</p> <p>Repositories are responsible for loading and saving Aggregate root entities, so consider the following example code:</p> <pre><code>using(IUnitOfWork uow = container.CreateUnitOfWork()) { Repository&lt;ARoot&gt; roots = container.GetRepository&lt;ARoot&gt;(); ARoot root = root.FindByName("ARoot"); root.Name = "ANewName"; roots.Save(root); uow.Commit(); } </code></pre> <p>The unit of work interface would be defined with the following methods:</p> <pre><code>public interface IUnitOfWork { void Insert(object); void Update(object); void Delete(object); void Commit(); void Rollback(); } </code></pre> <p>Lets say the repository is implemented using a very straightforward SQL Mapper, so the FindByName contains some direct SQL to return ARoot, would the Save implementation look something like this:</p> <pre><code>public void Save(T entity) { IUnitOfWork uow = GetUnitOfWork(); // Tell the UOW we are updating this entity uow.Update(entity); } </code></pre> <p>The Unit Of Work Commit code would then construct all the required SQL to map the entity back into the DB?</p> <p>Question 2)</p> <p>If I add an aggregate root into the Unit of work, is the unit of work responsible for persisiting the root, and its child enitities, or should be the Save method of the repository add the changed entities into the Unit Of Work? e.g</p> <pre><code>public void Save(T entity) { IUnitOfWork uow = GetUnitOfWork(); // Tell the UOW we are updating this entity uow.Update(entity); uow.Update(entity.AChildObject); } </code></pre> <p>OR... Alternativly</p> <p>Does a Unit of work only deal with aggregate roots, and when commited calls the repository Save methods for each object in its change set, keeping the SQL mapping code to persist the entity in the Repository, changing the first code example to</p> <pre><code>using(IUnitOfWork uow = container.CreateUnitOfWork()) { Repository&lt;ARoot&gt; roots = container.GetRepository&lt;ARoot&gt;(); ARoot root = root.FindByName("ARoot"); root.Name = "ANewName"; //roots.Save(root); uow.Update(root); // and commit uow.Commit(); } </code></pre> <p>Thanks,</p> <p>James</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.
    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