Reputation: 460
I'm playing with DDD and this question pop up. How I load child Aggregate Roots? Several performance issues would arise. Imagine the following example:
public AggregateRoot1
{
#region
properties
#endregion
public AggregateRoot2 AR2{get;set;}
public IEnumerable<AggregateRoot3> AR3List{get;set;}
(...)
}
If I load AggregateRoot2 and AggregateRoot3 list when I get AggregateRoot1, the graph would me massive. This not seem a nice approach.
I have two options:
BTW, I'm not considering using lazy loading.
I'm looking forward to ear your opinion about child AR's loading. Thanks
Upvotes: 2
Views: 2527
Reputation: 1948
If the graphs are getting too massive and you can't use lazy loading, it's probably a sign that your model could use some work - you likely have entities which should properly be their own aggregate roots.
By using factories and repositories, large objects can be bettered managed. You can cache large objects, or implement a singleton pattern within the factory of AggregateRoot1.
One reason to follow DDD is the encapsulation of complexity. But having IDs of non-root objects breaks this encapsulation. While there can be performance considerations, prematurely optimizing code for performance does not often create good software.
Upvotes: 0
Reputation: 37729
Ideally, references between aggregates should be by identity only. This is option 1. However, you should evaluate each reference to see if it is required for consistency of the reference holding aggregate. Sometimes, the relationship between two aggregates can itself be made an aggregate to be loaded individually. Overall, take a look at Effective Aggregate Design by Vaughn Vernon for in depth analysis of the various trade-offs when designing aggregates. This is also what David Masters points to in the linked question.
Upvotes: 7