Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Domain Driven Design - Aggregate Roots

I'm struggling with aggregates and aggregate roots. I have a natural aggregate root which works for about 60% of the user requests. I.e. those requests naturally apply to the aggregate root.

Within my aggregate I have another entity which can only exist as a member of the aggregate root. Users, however, will be told about this other entity object. It will sometimes make sense, conceptually, for users to operate on this non-aggregate root object directly.

So, I think I have a couple of options:

  1. They can they both be aggregate roots depending on which operation is being requested by the user.
  2. All operations have to go through the top level aggregate root.

Note that the top level aggregate root will hold a collection of this other entity.


Example:

Main aggregate root: Car

Second entity: Seat (a Car has either 2 or 4 seats depending on type). In my domain seats can only exist as part of a car.

Most operations in the domain are at the Car level. So that will be a good candidate for aggregate root. However, (and I'm struggling for examples here), some operations will be at the seat level, e.g. SpillCoffee, ChangeFabric, Clean....

Can Seat and Car both be aggregate roots? Or should I always start with Car?

Thanks

like image 763
ng5000 Avatar asked Jun 10 '09 16:06

ng5000


People also ask

What are aggregate roots in DDD?

An aggregate root is a class which works as an entry point to our aggregate. All business operations should go through the root. This way, the aggregate root can take care of keeping the aggregate in a consistent state. The root is what takes cares of all our business invariants.

What's an aggregate root?

An Aggregate is the clump of related entities to treat as a unit for data changes. The Aggregate Root is the main entity that holds references to the other ones. It's the only entity in the clump that is used for direct lookup.

How do you choose the aggregate root?

When choosing an aggregate root you choose between Transactional Consistency and Eventual Consistency. When your business rules allow you would rather favour Eventual Consistency.

What are aggregates in a domain model?

An aggregate is a group of objects that must be consistent together, but you cannot just pick a group of objects and label them an aggregate. You must start with a domain concept and think about the entities that are used in the most common transactions related to that concept.


2 Answers

The idea of an aggregate is to guarantee consistency, being the root responsible for data integrity and forcing invariants.

Suppose there's a rule like "The fabric of all seats must be the same", or ""you can only spill coffee on the seat if there's someone inside the car". It will be much harder to enforce these, once the clients will be able to change the fabric separately, or these invariants will need to be forced outside (danger zone).

IMHO, if integrity or forcing invariants is not an issue, then aggregates are not really needed. But, if it is necessary, my advice is to start everything with the car. But always think of the model. If there are invariants like these, then who enforces these invariants? Then try passing this idea to the code, and everything should be fine.

like image 80
Samuel Carrijo Avatar answered Sep 21 '22 13:09

Samuel Carrijo


Probably you need some deeper knowledge of some aspect of the domain model. This question shows that you are about to invent a way to organize the entities to supply the system, when, ideally, this kind of questions are already answered before implementation.

When this pops out only on the system implementation, you whether go back to review the domain or you discovered some fragility whose feedback could - and should - aggregate changes on related details of the business to make the domain richer and better modeled.

In the car example, I used the approach of two aggregates who correlate different contexts. The first would be the "car has a seat" approach, and in this aggregate the possible actions for "seat" would be just the ones that make sense to "seat as part of a car". Example: Clean.

The second aggregate would be in the context of "seat", and there would be the possible actions and configurations for seat as a standalone. Example: ChangeFabric, ColorList. This way, the aggregate "car" has "seat", but the clients can know seat on the context that makes sense. Which is dangerous, like said by samuelcarrijo on previous post. If the modifications between contexts affects the domain integrity, you lost all the aggregate concept.

like image 42
Rogerio Avatar answered Sep 21 '22 13:09

Rogerio