Note that there are some explanatory texts on larger screens.

plurals
  1. POAggregate Root references other aggregate roots
    primarykey
    data
    text
    <p>I'm currently working a lot with DDD, and I'm facing a problem when loading/operating on aggregate roots from other aggregate roots.</p> <p>For each aggregate root in my model, I also have a repository. The repository is responsible for handling persistence operations for the root. </p> <p>Let's say that I have two aggregate roots, with some members (entities and value objects).</p> <p>AggregateRoot1 and AggregateRoot2.</p> <p>AggregateRoot1 has an entity member which references AggregateRoot2.</p> <ol> <li>When I load AggregateRoot1, should I load AggregateRoot2 as well?</li> <li>Should the repository for AggregateRoot2 be responsible for this?</li> <li>If so, is it okay for the entity in AggregateRoot1 to call the repository of AggregateRoot2 for loading?</li> </ol> <p>Also, when I create an association between the entity in AggregateRoot1 to AggregateRoot2, should that be done through the entity, or through the repository for AggregateRoot2?</p> <p>Hope my question makes sense.</p> <p><strong>[EDIT]</strong></p> <p><strong>CURRENT SOLUTION</strong></p> <p>With help from <a href="https://stackoverflow.com/users/35389/twith2sugars">Twith2Sugars</a> I've come up with the following solution:</p> <p>As described in the question, an aggregate root can have children that have references to other roots. When assigning root2 to one of the members of root1, the repository for root1 will be responsible for detecting this change, and delegating this to the repository for root2.</p> <pre><code>public void SomeMethod() { AggregateRoot1 root1 = AggregateRoot1Repository.GetById("someIdentification"); root1.EntityMember1.AggregateRoot2 = new AggregateRoot2(); AggregateRoot1Repository.Update(root1); } public class AggregateRoot1Repository { public static void Update(AggregateRoot1 root1) { //Implement some mechanism to detect changes to referenced roots AggregateRoot2Repository.HandleReference(root1.EntityMember1, root1.EntityMember1.AggregateRoot2) } } </code></pre> <p>This is just a simple example, no Law of Demeter or other best principles/practices included :-)</p> <p>Further comments appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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