我应该如何将对象添加到由聚合根维护的集合中

Asa*_*vid 13 domain-driven-design

假设我有一个BlogPost聚合根.它拥有一份清单<Comment>.
BlogPost AddComment签名应如何显示?可以使用:

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

或者我应该避免在其之外创建对root用户的引用,并执行以下操作:

public void AddComment(string text, string email)
{
    Comment comment = new Comment(text, email);
    Comments.Add(comment);
}

moo*_*000 6

如果您相信DDD,那么只要您不在聚合之外的某处存储ID或引用,就可以了解聚合根目录下的某个实体.

我会选择blogPost.AddComment(new Comment(...))-version.


mbi*_*ard 6

如果您认为Comment是BlogPost的集合,并且在该范围之外没有意义,那么您应该使用第二个示例.

聚合根应控制聚合实例化的方式,以便它们的构造函数不应在聚合根之外可见.

另外,如果您想要一个真正的AggregateRoot-Aggregate关系,则Comment应该是BlogPost的子类.