Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I allowed to have "incomplete" aggregates in DDD?

DDD states that you should only ever access entities through their aggregate root. So say for instance that you have an aggregate root X which potentially has a lot of child Y entities. Now, for some scenario, you only really care about a subset of these Y entities at a time (maybe you're displaying them in a paged list or whatever).

Is it OK to implement a repository then, so that in such scenarios it returns an incomplete aggregate? Ie. an X object who'se Ys collection only contains the Y instances we're interested in and not all of them? This could for instance cause methods on X which perform some calculation involving the Ys to not behave as expected.

Is this perhaps an indication that the Y entity in question should be considered promoted to an aggregate root?

My current idea (in C#) is to leverage the delayed execution of LINQ, so that my X object has an IQueryable to represent its relationship with Y. This way, I can have transparent lazy loading with filtering... But getting this to work with an ORM (Linq to Sql in my case) might be a bit tricky.

Any other clever ideas?

like image 403
Fredrik Kalseth Avatar asked Aug 24 '08 13:08

Fredrik Kalseth


People also ask

Can an entity be part of multiple aggregates?

As you rightly pointed out, Entity instances shouldn't be shared between aggregates, as one aggregate wouldn't be aware of changes to the entity made through another aggregate and couldn't enforce its invariants.

Can a bounded context have multiple aggregates?

Importantly, the team is responsible for multiple Aggregates; thus, the Bounded Context contains multiple Aggregates. In some cases, an Aggregate may be large or complex enough that it alone is managed by a team. However, an Aggregate should never span Bounded Contexts.

How are aggregates defined in DDD?

First, let's take a look at the definition of aggregation in the DDD Reference: Entity and value objects are divided into aggregation and define the boundary around the aggregation. Select an entity as the root of each aggregation and allow only external objects to hold references to the aggregation root.

Can an aggregate reference another aggregate?

[Evans] states that one Aggregate may hold references to the Root of other Aggregates. However, we must keep in mind that this does not place the referenced Aggregate inside the consistency boundary of the one referencing it. The reference does not cause the formation of just one whole Aggregate.


1 Answers

I consider an aggregate root with a lot of child entities to be a code smell, or a DDD smell if you will. :-) Generally I look at two options.

  1. Split your aggregate into many smaller aggregates. This means that my original design was not optimal and I need to identify some new entities.
  2. Split your domain into multiple bounded contexts. This means that there are specific sets of scenarios that use a common subset of the entities in the aggregate, while there are other sets of scenarios that use a different subset.
like image 146
Stefan Moser Avatar answered Oct 22 '22 07:10

Stefan Moser