DDD and Homogeneous Many-to-Many Relationship

后端 未结 3 1641
梦谈多话
梦谈多话 2021-02-06 16:44

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 <

3条回答
  •  后悔当初
    2021-02-06 17:30

    That would very much depend on where you consistency boundaries need to be. Which would also therefore depend on what business rules you have.

    While Meta-Knight has FriendRequest in the same Aggregate I would have it as its own and use events to communicate between the Aggregates therefore making a Person and there FriendRequests eventually consistent. This would allow you to do something like.

    public class DomainRouter {
        public void When(FriendRequestCreated event)
        {
                //Send command to each Person to record request
        }
    
        public void When(FriendRequestAccepted event)
        {
                //Send command to Person to record request accepted and add friend.
                //Send comamnd to Person who accepted to add Friend
        }
    
        public void When(FriendRequestDeclined event)
        {
                //Send command to update Friend request on person.
                //Send command to Person who declined to record they have declined?
        }
    }
    

    The information on Person would therefore just be a record of state. The FriendRequest Aggregate would be where all the process actually happens.

    What is important in DDD is to think about behavour. A FriendRequest can be Requested, Withdrawn, Accepted and Declined. What can a Person do? Does a Person need to be DDDd or could you make it nice and simply CRUD + store info into graph database etc.

    Maybe you want to model it that in a way where you can go person.requestFriendAcceptance(Person id) in which case the router would probably just handle the FriendRequestCreated event by notifying the friend of a FriendRequest.

提交回复
热议问题