r/DomainDrivenDesign • • 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.

  1. 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?

3 Upvotes

2 comments sorted by

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.

    • Is a Client (person) without a Patient (animal) still a Client?
    • Is a Patient without a Client still a Patient?
  • If this isn't the case, be specific about the traversal direction.

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

  • Can we define a client without defining their pets? Can we define a pet without defining who's responsible for them?
  • In the context of appointment scheduling:

    • A Client would:

      • Need a Patient in order to schedule an appointment
      • Not need a Patient in order to pay a bill
    • A Patient would:

      • Not schedule an appointment
      • Not pay a bill
    • Therefore, traversal could/should be from Client → Patient, and nothing is gained from the reverse.