Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should rules for Aggregate Roots be enforced?

While searching the web, I came across a list of rules from Eric Evans' book that should be enforced for aggregates:

  1. The root Entity has global identity and is ultimately responsible for checking invariants
  2. Root Entities have global identity. Entities inside the boundary have local identity, unique only within the Aggregate.
  3. Nothing outside the Aggregate boundary can hold a reference to anything inside, except to the root Entity. The root Entity can hand references to the internal Entities to other objects, but they can only use them transiently (within a single method or block).
  4. Only Aggregate Roots can be obtained directly with database queries. Everything else must be done through traversal.
  5. Objects within the Aggregate can hold references to other Aggregate roots.
  6. A delete operation must remove everything within the Aggregate boundary all at once
  7. When a change to any object within the Aggregate boundary is committed, all invariants of the whole Aggregate must be satisfied.

This all seems fine in theory, but I don't see how these rules would be enforced in the real world.

Take rule 3 for example. Once the root entity has given an exteral object a reference to an internal entity, what's to keep that external object from holding on to the reference beyond the single method or block?

(If the enforcement of this is platform-specific, I would be interested in knowing how this would be enforced within a C#/.NET/NHibernate environment.)

like image 796
MylesRip Avatar asked Mar 17 '10 23:03

MylesRip


1 Answers

I don't think you should let the aggregate give your external code access to it's entities.

You tell your aggregate what you want to happen and it deals with it.

If we have an aggregate:Car. We don't care about petrol, and wheels, we just drive. We ask the car about things and it answers without giving references to the internals.

We ask: Do we have petrol? Yes. Not: Give me the tank object so I can check if we have petrol.

like image 98
Rickard Nilsson Avatar answered Sep 18 '22 05:09

Rickard Nilsson