Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

domain driven design - how should i add an object into a collection maintained by aggregate root

lets say i have a BlogPost aggregate root. it holds a List<Comment>.
how should the BlogPost AddComment signature look? is it OK to use:

public void AddComment(Comment comment)
{
    Comments.Add(comment);
}

or should i avoid creating references to root's children outside of it, and do something like this:

public void AddComment(string text, string email)
{
    Comment comment = new Comment(text, email);
    Comments.Add(comment);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you consider Comment to be an aggregate of BlogPost and to not make sense out of that scope then you should be using the second example.

The aggregate root should control how the aggregates are instantiated so their constructors should not be visible outside of the aggregate root.

Plus, Comment should be a child class of BlogPost if you want a true relation of AggregateRoot-Aggregate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...