Imagine you build a social network. There are users who can add other users as friends. How do you model this in DDD? Naturally you cannot simply have a list of friends in the <
hmm... Actually it's quite easy to do what you've asked, and your situation is a standard one. You don't have to store actual User
object in Friendslist
of your User
aggregate, just put there IDs of users who are friends for the User
.
This is one of the rules of aggregate implementation proposed by Vaugh Vernon: link to other aggregate and entities by their ID. So no loops, just list of IDs.
In that situation then somebody become a friend to somebody you have to change two aggregates at one time. It can be undesirable behavior because change can't occur instantly in one transaction. But for this case you have Domain Events and friend requests can be easily modeled: your aggregates can communicate with each other with FriendshipRequested
, FriendshipAccepted
, FriendshipCancelled
or FriendshipDeclined
events and change their state correspondingly.
In this case you also receive logs and notifications for free.