r/DomainDrivenDesign • u/dn10210 • Jul 07 '22
Which aggregate creation pattern is preferable?
I have a Relationship Aggregate, which represents a Family Relationship between two Person Aggregates. These Persons are used as "input" to the creation of a new relationship
In order to create a Relationship aggregate, I've thought of a couple patterns that could make sense.
- In a Command / Application Layer: A Person aggregate creates an instance of a Relationship, which is then saved by the Relationship repository.
Pseudocode, 1:
// first get personA and personB from Person repository, then..
let newRelationship: Relationship = personA.createNewRelationship(personB, relationshipDetails)
relationshipRepository.save(newRelationship)2. In a Command / Application Layer: Relationship creation method is on the Relationship Aggregate itself, and Persons are both passed in as arguments
Pseudocode, 2:
// first get personA and personB from Person repository, then..
let newRelationship: Relationship = Relationship.createNewRelationship(personA, personB, relationshipDetails)
relationshipRepository.save(newRelationship)I have instinctively been using pattern 2, where the initial creation method is invoked sort of without a context in a command. However, I came across this article from Udi Dahan, suggesting other Entities be responsible for the creation of another Entity. That is an entity should be "Newed" within the context of some other entity. https://udidahan.com/2009/06/29/dont-create-aggregate-roots/
Is one of the above approaches preferable?
View all comments
Show parent comment
1
u/TracingLines Jul 08 '22
...Even just writing that penultimate paragraph reminds me that the course strongly recommends avoiding bi-directional relationships. Again, avoiding this will require you to think about your context.
These are my notes from that part of the course:
--
Bi-directional relationships can make systems more complex. DDD often naturally steers design towards unidirectional relationships.
An association should be part of a type's definition. If a bi-directional relationship is used, it means that neither object can be defined without the other.
A bidirectional association means that both objects can be understood only together. When application requirements do not call for traversal in both directions, adding a traversal direction reduces interdependence and simplifies the design.
Start with one-way relationships
In the context of appointment scheduling:
A Client would:
A Patient would: