Link to an object by reference to Id in DDD aggregates

The Pluralsight Domain- Based Design Fundamentals course provides an example of how an aggregate structure is formed. An example involves appointing patients to the clinic. An appointment has a relationship, for example. to the doctor or to the examination room. And this example is preceded by an analysis that Assignment should not be an aggregated root for Doctor and ExamRoom. And one step in the evolution of design comes from the Destination, which has object references to the Doctor and ExamRoom objects, to the primitive identifier of these other objects, DoctorId and ExamRoomId. They motivate this change by saying: โ€œBy simply including identifiers of related concepts, rather than references to objects, we can ensure that the creation and change of the Assignment has a minimal impact on our system when we save our assignmentโ€

My first question is: is this a common design template? If I understood correctly, he would generalize to something like: If object A refers to object B, but one working on A should never make changes to B, then refer to it by its identifier, and not by B. what would you recommend?

My second question is: Does this have anything to do with DDD? I mean the fact that the Appointment should not be the cumulative root of the doctor, does not mean that it cannot contain references to objects, or am I missing something?

+8
domain-driven-design
source share
1 answer
  • I think this is a general design pattern, at least in the DDD universe. Evans says in DDD:

The root is the only member of AGGREGATE that external objects are allowed to hold references to

If you are using some kind of ORM, such as Hibernate, you may have to deal with lazy loading in order to deal with deeply related object structures that have object references. Some people find that lazy loading is an anti-template.

Check out this QA for a better understanding of the concept of aggregates. Personally, I am convinced that clearly defined aggregate boundaries improve your architecture.

  1. If you implement aggregated boundaries, your type of appointment will most likely not have direct references to the object for the doctor.

UPDATE: Vaughn Vernon talks about the rules that describe the current consensus view of DDD leaders on aggregate style (see Part II):

[DDD] indicates that one aggregate may contain links to the root of other aggregates. However, we must remember that this does not place the reference aggregate within the consistency boundary, one of which refers to it. Link does not call one, the whole population.

He continues:

If you modify multiple instances in one transaction, this can be convincing proof of the incorrectness of your consistency boundaries. If so, perhaps this is a missed modeling opportunity; the concept of your ubiquitous language has not yet been discovered, although it waves its arms and screams at you (see Part I).

In my understanding, an Assignment should not contain references to direct objects to other aggregate roots, such as Doctor.

+5
source share

All Articles