Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are entity classes exclusive to one aggregate

After reading Evans and Vernon I still have one fundamental Question. I'm aware that of course one entity (instance) can only be in one aggregate. But can an entity class be used in multiple aggregates (classes)? For clarification, I ask on the class level. Other formulation: Can two different aggregate root classes (!) aggregate the same entity class? Of course any of the entity instances has to belong to only one instance of one of the two aggregate root classes. For Value Object classes this seems to be possible. At least I have the impression that a value object class for example for "money" can be used in different aggregate types.

like image 467
Holger Thiemann Avatar asked Mar 04 '15 11:03

Holger Thiemann


People also ask

What is the difference between an aggregate and entity?

Entity has meaning (and therefore an id) defined outside of the state of its values as oppose to "value objects" whose identity is defined entirely by its state. Aggregate a structure of internally consistent and logically related "things" which might be entities or value objects.

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.

What is an aggregate in Domain Driven Design?

Aggregates are guards for business principles in domain implementation. They merge several contexts into one, transparent object which unifies the interface for aggregated stuff. For example we can have many entities and value objects nested in aggregate.

What is the difference between entity and value object?

A Value Object is an immutable type that is distinguishable only by the state of its properties. That is, unlike an Entity, which has a unique identifier and remains distinct even if its properties are otherwise identical, two Value Objects with the exact same properties can be considered equal.


1 Answers

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.

Entity classes could theoretically be shared between 2 aggregates, but, by the same reasoning, only if the set of entity instances in an Aggregate is disjoint from the other. This raises questions :

  • Why would you want that in the first place ? If there are two big categories of instances of the same class, isn't this a sign that there are two semantically different concepts, which should each have their own class, or at least subclass ?

  • How do you prevent an entity instance belonging to one aggregate from being added to the other, at runtime (bug), or at programming time (uneducated developer decision) ?

Value Objects escape these issues because they are usually immutable or treated as such -- you don't modify a VO, you modify its parent Entity so that it points to a whole new VO instance. Also, as Value Objects don't have an identity, it doesn't make much sense to say that the "same" VO is in two aggregates at the same time. You can thus safely reuse a VO type in different aggregate classes.

like image 127
guillaume31 Avatar answered Oct 12 '22 07:10

guillaume31