如何将对象添加到由聚合根维护的集合中 [英] how should i add an object into a collection maintained by aggregate root
问题描述
让我说我有BlogPost聚合根。它包含一个List < Comment>
。
BlogPost AddComment签名应如何显示?是否可以使用:
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);
}
或者我应该避免在其外部创建对root的孩子的引用,并执行以下操作:
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);
}
推荐答案
如果您相信DDD,那么最好了解一些实体根下的实体,只要您不在集合外的某个地方存储ID或对其的引用。
If you believe in DDD, it's perfectly fine to know about some entity beneath the aggregate root, as long as you do not store an ID or a reference to it somewhere outside of the aggregate.
我会选择 blogPost.AddComment(new Comment(...))
-version。
I would go for the blogPost.AddComment(new Comment(...))
-version.
这篇关于如何将对象添加到由聚合根维护的集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!