How to submit links in DDD entites

I am trying to get a DDD descriptor and feel that I have a pretty good understanding of entities, aggregates, aggregate roots, repositories, and value objects and how to actually represent these concepts in code.

What I'm still struggling with is representing the links (for example, relationships that do not constitute the totality) in real code. Let them say that I have the following two units:

DISCUSSIONGROUP (entity, aggregate root)
 +name: string
 +ENTRY (entity)
   +title: string
   +message: string
   +timestamp: date
   +madeByUser: USER

USER (entity, aggregate root)
 +name: string
 +email: string
 +memberOf: DISCUSSIONGROUP

A user can be a member of a discussion group, but the user object does not belong to the same aggregate. However, there is a connection between the two (link). When you look at the actual code, I would do aggregate relationships like (using C #):

interface IDiscussionGroup {
    string Name { get; }
    IList<IEntry> { get; }
    ...
}

Ie using simple C # links. Do you make DDD links (as described above) I in the same way, that is:

interface IUser {
    IDiscussionGroup MemberOf { get; }
    ...
}

- ?

+3
2

, . , , madeByUser DISCUSSIONGROUP ValueObject.

? DisscusionGroup , , . , , . , , , , ValueObject . , VO , .

, , ? , Entity (, Front-office) backoffice ( , , disscussion). DisscussionGroup VO . ? , Value?

, , - VO .

Personnaly , , , Lazy Loading stuf, .

, .

+1

" -" " -" .

- , .

public class User{
  public IList<DiscussionGroup> GroupMembership{get; private set;}
  public User(){
    GroupMembership=new List<DiscussionGroup>();
  }
  public void JoinGroup(DiscussionGroup group){
    GroupMembership.Add(group);
  }
  public bool IsMemberOf(DiscussionGroup group){
    return GroupMembership.Contains(group);
  }
  public void EnsureMembership(DiscussionGroup group){
    ThrowIf(!IsMemberOf(this),
      "User is not a member of this discussion group");
  }
}
public class DiscussionGroup{
  public IList<Discussion> Discussions {get;private set;}
  public DiscussionGroup(){
    Discussions=new List<Discussion>();
  }
  public Discussion CreateDiscussion(string name, Post firstPost){
    CurrentUser.EnsureMembership(this);
    var discussion=new Discussion(this, name, firstPost);
    Discussions.Add(discussion);
    return discussion;
  }
}
public class Discussion{
  public DiscussionGroup Group{get; private set;}
  public Discussion(DiscussionGroup group, string name, Post firstPost){
    CurrentUser.EnsureMembership(group);
    Guard.Null(group);
    Group=group;
    Name=name;
    Posts=new List<Post>{firstPost};
  }
  public void WritePost(string text){
    CurrentUser.EnsureMembership(group);
    Posts.Add(new Post(this,text));
  }
}
public class Post{
  public Discussion Discussion{get; private set;}
  public string Text {get; private set;}
  public Post(Discussion discussion, string text){
    Guard.Null(discussion);
    Discussion=discussion;
    Text=text;
  }
}

//usage
var me=new User();
var you=new User();
var stackOverflow=new DiscussionGroup();
me.JoinGroup(stackOverflow);
you.JoinGroup(stackOverflow);

LoginAs(you);
var question="I'm trying to get a handle on DDD and feel that...";
var discussion=stackOverflow.CreateDiscussion
  ("How to represent references in DDD entites",question);

LoginAs(me);
var answer="'Holds something' and 'belongs to something'...";
discussion.WritePost(answer);
0

All Articles