Reputation: 19715
In my journey to try to learn DDD, I came with a simple problem. I have an aggregate root Assistant. That assistant can manage therapists.
so I have:
var assistant = GetAssistant(Id);
var therapist = new Therapist("[email protected]");
assistant.Manage(therapist);
repository.Save(assistant);
the business rules says that if the therapist [email protected] is already managed by any assistant, then it cannot be added.
But as far as I understand, assistant doesn't have to know about the therapists of other assistants. So how can I make this in DDD?
UDATE Therapist is Entity. Assistant is Entity.
Assistant must be a user in the system. Therapist may be a user in the system.
A therapist has a schedule and create appointments, etc, but also can have an assistant that does that work for her/him.
There can be an assistant as a user that manage other therapist (users or not).
So the entry to the system is by registering a User. Then, you can add a Therapist (yourself or another) If you add another therapist for you to manage, I want to be sure that: a) Therapist doesn't already exist on the system.
I think I got my AR all wrong. But if my user is the AR, I face the same issue were how can a user know about other data from the system that is supposed to not know?
I really appreciate your help, I'm banging my head hard with this.
Upvotes: 0
Views: 92
Reputation: 7283
From the implementation point of view. You may write the code as:
var assistant = GetAssistant(Id);
var therapist = GetTherapist("[email protected]");
therapist.ManagedBy(assistant);
repository.Save(therapist);
The therapist knows if he/she is managed by an assistant, so the rule is applied here. And the therapist can handle concurrency contention with locks.
On the other side, the therapist may not exist yet. I think this is more a UI convenience concern (find or create issue).
Upvotes: 1