So basically the idea of DDD aggregation is equivalent to the idea of object-oriented composition?
No . It is very common to try to understand the DDD design patterns by confusing them with object-oriented design patterns; and this usually leads to deception.
Although in practice what we see most is DDD being implemented using object orientation (lie, in practice we almost do not see DDD) , strictly object orientation is not even a requirement - DDD can be implemented using other paradigms.
What are aggregations really?
In DDD, aggregation is the structuring of entities and objects of value in a cohesive way in order to guarantee the expressivity of the domain and the integrity of these objects, since access to them can only be done from root .
How do we identify aggregations when making the domain template?
Let's take a look at the basic structure of an aggregation:
-> Entidade Raiz
-> Entidade agregada
-> Objetos de valor
-> Entidade agregada
-> Objetos de valor
-> Objetos de valor
-
Value objects: They do not have any identity, so they will always be part of an entity. They will be part of an aggregation when your entity is the root of this aggregation or your entity is part of this aggregation.
- Entities : they have global identity in the domain (or global in context in the case of a domain with more than one model - see bounded context). That is, you are able to differentiate one object from another by its identity and not just by its characteristics.
-
Entities that are part of an aggregation : they have identity, but not global. To make sense in the domain, their identity must be preceded by the identity of another entity (a "parent entity", so to speak).
- Root entities : An entity with a global identity will be the root of an aggregation if it depends on other entities (which do not have a global identity) to help describe it. These other entities will then be aggregated to this root entity - and hence we have an aggregation.
The domain determines which objects have identity and which have global identity. Those with global identity may be the root of aggregation and those whose identity is dependent on this root entity will be aggregated to it. Value objects, in turn, are always close to your entity.
Aggregation example
An example in a certain domain or model could be:
-> Livro (entidade raiz)
-> Reviews de clientes (entidades agregadas)
-> Reviews editoriais (entidades agregadas)
-> Formatos disponíveis - papel, kindle, pdf (objetos de valor)
In this case, the workbook has a global identity in the domain or template. Your identity is your name plus the name of its author.
Reviews also have identity, which is the name of the client or publisher, and possibly the date of publication. But see that the identity of a review only makes sense if preceded by the identity of the book itself - although it is important to identify each review of a book (who wrote the review and when), it serves no more than one book and will not exist without a book.
Available formats have no identity - a book is simply available in some of the existing formats.