DDD implementation

domain-driven-design

Solution

My thought is that this is an example of what in relational database theory is termed a "weak entity". A `Friendship` could be identified solely by the identifiers of the two `User`s involved in the friendship, but could have its own properties such as when it was created and what type of relationship it is.

I would make this its own entity, and probably hide it behind a facade exposed by the `User` object:

class User {
    protected List<Friendship> _friendships { get; private set; }

    public IEnumerable<User> Friends {
        get { return _friendships.Select( /* get other user */ ); }
    }

    public void AddFriend(User otherUser) {
        // check to see if friendship exists
        // if not, create friendship
        // do other friendshippy things

        // make sure the other user knows about our friendship 
        // and gets to do its friendshippy things
        otherUser.AddFriend(this);
    }
}

Problem

I'm new to Domain Driven Design and I have some doubt about some concepts (hoping that this is the right place to ask this). I know that in DDD I should avoid an anemic model, so thinking about a social network model who should make (save) the friendship between two friends? I imagine the situation like having a class representing the Users (using a Java-like syntax): ``` class User{ String username List<User> friends } ``` So, should it have a method to add a friend? ``` class User{ void friendship(User friend) } ``` or should I have to use a service to do it? ``` class UserService{ void friendship(User user, User user2) } ```

Original source

Related problems