【发布时间】:2019-07-26 11:19:11
【问题描述】:
我一直在阅读有关 DDD 的内容,但仍然对聚合根感到困惑。 想象一下,我有一个类似于博客的情况,人们可以在其中创建帖子并将 cmets 添加到其他帖子。
规则: - 每个人都需要有一个帐户才能添加帖子或评论 - 用户只能删除自己的 cmets
考虑到这一点,我需要以下对象: -邮政 -发表评论 -用户
所以,我只创建了 Post 对象作为聚合根,并为其添加了一些业务逻辑
public class User : EntityBase
{
public string Name { get; set; }
public string Avatar { get; set; }
}
public class Post : EntityBase, IAggregate
{
public string Title { get; set; }
public string Content { get; set; }
public User Creator { get; set; }
private IList<PostComment> Comments { get; set; }
public void AddComment(PostComment comment)
{
this.Comments.Add(comment);
}
public void DeleteComment(PostComment comment, int userId)
{
if (comment.Creator.Id != userId)
throw new Exception("You cannot delete a comment that is not yours. blablabla");
this.Comments.Add(comment);
}
public IList<PostComment> GetComments()
{
return this.Comments;
}
}
public class PostComment : EntityBase
{
public string Comment { get; set; }
public User Creator { get; set; }
}
我这样做正确吗?我的意思是,业务逻辑是否在正确的位置?或者我也应该将 PostComment 设为聚合根并在其中添加添加/删除的逻辑?
【问题讨论】: