I'm trying to get a handle on DDD and feel that I've got a pretty good grasp regarding entities, aggregates, aggregate roots, repositories and value objects and how to actually represent these concepts in code.
What I'm still struggling with is how to represent references (i.e relations that don't make up an aggregate) in actual code. Let's say I have the two following aggregates
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 entity don't belong in the same aggregate. There is still however a relation between the two (a reference). When looking at actual code, I would implement aggregate relations such as (using C#):
interface IDiscussionGroup {
string Name { get; }
IList<IEntry> { get; }
...
}
I.e using simple C# references. Would you implement DDD references (as described above) i the same manner, i.e:
interface IUser {
IDiscussionGroup MemberOf { get; }
...
}
of should one use some other construct to indicate the weaker type of relation?
Really it depends on your requirements. There is no one way of doing it well but what I would do is to make a madeByUser property in DISCUSSIONGROUP entity a ValueObject.
Why ? Because when displaying Entries from DisscusionGroup I'm rather interested about the name and maybe email of the user to display on the entry. I don't want that user disapears on my entry when it's deleted from the database. If the User entity had more properties and behaviours, I'm not interested in that when displaying user information for example so the ValueObject will be fine. More, you can share that VO between many entries without retriving the instance of concrete User for each entry.
On the other hand is User aggregate used in the same context as the discussionGroup ? What I mean that the DiscussionGroup could be an Entity in one context (for example in the Front-office use case) and a value object in user managment backoffice (we're not interested in managing disscussion groups but the users only, so a simple information about disscussiongroup is sufficient). However you may want to have a reference from DisscussionGroup entity to User entity or make also a DiscussionGroup a VO in that case. Do you need to retrieve all discussionGroups and entries for a given user ? Or you need just a few information that you could embeded in a Value Object?
I'm sorry if I didn't answer your question very well but doing a direct references or just using some kind of shared Id between the aggregates or using VO depends really on the uses cases and the behaviour you have to implement.
Personnaly I check if I definied a good aggregate boundaries if I can retrieve all data in the aggregate without doing Lazy Loading and other stuf like this.
Maybe others have another point of view.
"Holds something" and "belongs to something" looks kind a similarly.
Difference is - when what is constructed.
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);
精彩评论