Yes, Appointment
should not be an AR for Doctor
and ExamRoom
because that doesn't form the best boundaries for the business being modelled.
Creating an appointment for example, shouldn't take care of validating a whole model that includes the doctor and the exam room (business invariants/rules).
The other two entities would be better off being modelled as separate ARs of themselves. This decouples the system and allows the three models to operate separately, allowing much better concurrency when dealing with all three models at the same time, for example by 3 different users working concurrently on the 3 different aspects (ie. models).
Remember when they asked the customer if more than one person will schedule appointments at one time? She said no, but it's not excluded that at one time in the near future, they will want to allow that as well.
That gave Steve and Julie motive to start simple, with an initial version of the domain model that modelled everything in one huge aggregate... perhaps on purpose, so they could later make two important points for DDD: refactoring and designing small aggregates.
So to answer your first question, it would be better for Appointment
(an AR) to hold an Id as a reference to Doctor
(as an AR), and not an object reference.
While holding an object reference could seem fine at first, because after all Doctor
, in this case, is an AR and is encapsulated and responsible for its own invariants, it can easily end up that the doctor object is no longer a reference to an external entity but an entity that is in whole part of the appointment AR, because it is easy to start calling methods on it and linking internal (Appointment
) logic to that, expecting some form of state consistency.
What happens then if another AR also holds a reference to the same doctor and starts doing the same, thinking it's the only AR responsible for the state of that doctor? Well you'd have a tightly coupled, broken, inconsistent model.
But in one situation, Appointment
(AR) can hold an object reference to Doctor
if the doctor is modelled as internal to (ie. a part of) the appointment aggregate. But then nothing outside can (and should not!) reference the doctor!
To answer your second question, which should probably be obvious by now: of course it has everything to do with (proper) DDD. Operating on Appointment
should not also operate on Doctor
because this would cross aggregate boundaries (you're saving multiple aggregate roots in one transaction). You should probably think then that maybe the current model is not the best one, or perhaps there is some hidden business rule that you haven't uncovered yet.
But the appointment can hold a reference to the doctor (by Id, of course). This part of your assumption was incorrect. But I guess it was obvious by now. It's part of the first question.
I saw this question while learning DDD and saw that it's from 2015 and it hasn't gotten an answer to your first comment on @mdo's answer, so I thought I should try and answer the question as well, but try and be less succint. Perhaps @mdo considered that the answer to your comment is also in his answer to your question. I can see why. DDD is confusing and hard to grasp. I'm still learning it myself. Anyway, hope I got it right... for me and everybody else reading this answer. 😅