19

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?

5 Answers 5

6

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.
2
  • 2
    It might be a code smell but it might also be not. I've got the same problem. I have a Trace class which holds geographical waypoints. Now this trace can get arbitrary long and while the Trace itself is an entity the waypoints are not. They are value objects. So if I want to update the Trace in the pure DDD sense, I would have to load the complete aggregate root Trace with all its waypoints, add some waypoints, and store the complete Trace again. This obviously doesn't scale for large traces. So due to performance issues I am faced with maybe allowing partially loaded aggregate roots...
    – Jonny Dee
    Commented May 4, 2012 at 9:35
  • I realize this answer is 11 years old... But I believe one is supposed to model their aggregates based on what domain experts say. So in case one misinterpreted what the domain experts said, a huge aggregate should be remodeled into smaller that match the vision of domain experts better. In case, a huge aggregate is consistent with the mental model of domain experts -- formally speaking DDD dictates it be huge. Bad luck, I guess. Actually, Eric Evans mentioned this problem and possible solutions in a talk: youtu.be/lE6Hxz4yomA?t=1778
    – Myk
    Commented May 5, 2019 at 23:17
4

Jimmy Nilsson hints in his book that instead of reading a complete aggregate you can read a snapshot of parts of it. But you are not supposed to be able to save changes in the snapshot classes to the database.

Jimmy Nilsson's book Chapter 6: Preparing for infrastructure - Querying. Page 226.

Snapshot pattern

2

Is it OK to implement a repository then, so that in such scenarios it returns an incomplete aggregate?

Not at all. Aggregate is a transnational boundary to change the state of your system. Never use aggregates for querying data. Split the system into Write and Read sides. (read about CQS & CQRS). When we think "CRUD" based, we implement our system, based on some resource. Lets say you have "Appointment" aggregate. Thinking "Crudish" means we should implement usecases Create, Update, Delete, GetAll appointments. That means Appointment[] should be returned for GetAll. When you think usecase based, (HexagonalArchitecture) your usecases would be ScheduleAppointment, RescheduleAppointment, CancelAppointment. But for query side it can be: /myCalendar. We return back all appointments for a specific user in a ClientCalendar object. Create separate DTO's for Query sides. Never use aggregates for this purpose.

1
  • 1
    This is the right answer and should be at the top.
    – Arad
    Commented Aug 15 at 9:43
1

You're really asking two overlapping questions.

  1. The title and first half of your question are philosophical/theoretical. I think the reason for accessing entities only through their "aggregate root" is to abstract away the kinds of implementation details you're describing. Access through the aggregate root is a way to reduce complexity by having a trusted point of access. You're eliminating friction/ambiguity/uncertainty by adhering to a convention. It doesn't matter how it's implemented within the root, you just know that when you ask for an entity it will be there. I don't think this perspective rules out a "filtered repository" as you describe. But to provide a pit of success for devs to fall into, it should be impossible instantiate the repository without being explicit about its "filteredness;" likewise, if shared access to a repository instance is possible, the "filteredness" should be explicit when coding in the caller.

  2. The second half of your question is about implementation on a specific platform. Not sure why you mention delayed execution, I think that's really orthogonal to the filtering question. The filtering itself could be a bit tricky to implement with LINQ. Maybe rather than inlining the Where lambdas, you set up a collection of them and select one depending on the filter you need.

0

You are allowed since the code will compile anyway, but if you're going for a pure DDD design you should not have incomplete instances of objects.

You should look into LazyLoading if you're afraid to load a huge object of which you will only use a small portion of its child entities.

LazyLoading delays the loading of whatever you decide to lazy-load until the moment they are accessed. They make use of callbacks to call the loading method once the code calls for them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.