帖子和评论应该在帖子聚合中还是应该单独聚合?
考虑带有对象 Post
和 Comment
的典型博客。
对于我一直在构建的 DDD 演示示例,我(到目前为止)发现实体 Post
和 Comment
都适用于同一聚合 - Post聚合。但现在我不太确定......
在我的控制器中,我发现,就像您所期望的那样,我需要从 Posts
添加和删除 Comments
。使用我当前的模型,我不会在全球范围内跟踪Comment
的身份(就像蓝皮书建议的那样)。您可能期望我删除 Comment
的操作可能如下所示:
public ActionResult DeleteComment(int postID, int commentID)
显然,我需要 Post
的 id 来从存储库中检索它以及特定的标识符对我要删除的帖子
进行评论
。
我的问题是 DeleteComment(
操作的主体:
是否可以使用查询机制遍历 Post
来获取要删除的 Comment
?像这样:
var comment = this._postRepo.WithID(postID).Comments
.SingleOrDefault(c => c.ID == commentID);
this._postRepo.Delete(comment);
return RedirectToAction("detail", new { id = postID });
..或者我应该从类似于这样的存储库中选择Comment
?:
var comment = this._postRepo.CommentWithID(commentID)
..或者:
var comment = this._postRepo.CommentWithID(postID, commentID)
上面的两个例子可能看起来有点愚蠢,因为我不需要如果我可以在全球范围内跟踪
ID 但是如果我在全球范围内跟踪Comment
,则发布Comment
,它不应该有自己的聚合吗?当 Post
和 Comment
似乎在一起时?
Consider the typical blog with objects Post
and Comment
.
For a DDD demo example i have been building i have (till now) found that both the entities Post
and Comment
have been appropriate for the same aggregate- the Post
aggregate. But now i'm not so sure..
In my controllers i am finding, like you would expect, that i need to add and remove Comments
from Posts
. With my current model i am not tracking the identity of a Comment
globally (like the Blue Book suggests). You you might expect that my action to delete a Comment
may look like this:
public ActionResult DeleteComment(int postID, int commentID)
Obviously i need the Post
's id to retrieve it from the repository and the identifier for the particular Comment
on that Post
that i want to delete.
My problem is the body of the DeleteComment(
action:
Is it ok to traverse the Post
with a query mechanism to get the Comment
for deletion? like this:
var comment = this._postRepo.WithID(postID).Comments
.SingleOrDefault(c => c.ID == commentID);
this._postRepo.Delete(comment);
return RedirectToAction("detail", new { id = postID });
..or should i be selecting the Comment
from the repo similar to this?:
var comment = this._postRepo.CommentWithID(commentID)
..or:
var comment = this._postRepo.CommentWithID(postID, commentID)
The two above examples might seem a little silly since i shouldn't need the Post
ID if i can track the Comment
globally. But then if i'm tracking the Comment
globally, shouldn't it have it's own aggregate and then is that right when Post
and Comment
seem to go together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,这在很大程度上取决于评论在帖子之外是否具有任何意义。我倾向于认为确实如此,原因有几个。首先,概念上可以在普通博客引擎中对帖子以外的事物进行评论(例如图像、新闻条目、其他评论)。其次,正如前面提到的,您经常会看到只有评论的小部件,这些小部件独立于他们的帖子。我还认为这种情况会让你正在苦恼的决定变得更加微不足道。
也就是说,如果您确实选择将它们设为一个聚合,那么请记住,存储库在进行查询时通常会加载整个聚合,并依靠缓存等机制来提高效率。因此,您的场景将是查询帖子,然后搜索该帖子的评论以查找要编辑/删除/其他内容的“正确”评论。
As others have said, it depends greatly on whether a comment has any meaning outside of a Post. I tend to think that it does, for several reasons. First, things other than posts can conceptually be commented upon in a normal blog engine (e.g. an image, a news item, another comment). Second, as was also brought up, you often see widgets of just comments, which are independent of their posts. I also think that this scenario makes the decisions you're agonizing over a bit more trivial.
That said, if you do choose to make them one aggregate, then remember that a repository will often load the entire aggregate when making queries, relying on mechanisms like caching and such to make that efficient. So your scenario would be a query for a post, followed by a search of that post's comments for the 'right' comment to edit/delete/whatever.
在我看来,评论应该是帖子聚合的一部分,但评论应该是实体,因为具有相同答案的两个评论仍然是两个单独的评论。
如果您将评论创建为单独的聚合,其中评论是根,则评论将具有存储方法,这意味着任何主体都可以创建评论,但基本思想是不应在没有帖子的情况下创建评论。评论与帖子相关。
如果你按逻辑思考,评论不可能进化成它自己的。这意味着当创建评论时,它应该成为帖子的一部分。
In my opinion , comment should be part of Post Aggregate, but comment should be entity, bcoz two comment with the same answer is still two separate comment.
If you create comment as separate aggregate where comment is root then, comment will have store method that means any body can create comment but the basic idea is comment should not be created without its post.Comment is related with post.
If you think logically , Comment cannot be evolve it's own. That means when commented is created it should be part of post.
问题是评论在帖子聚合之外是否具有任何意义。恕我直言,没有任何评论,所以我认为您不应该将评论移至其自己的聚合中。
The question is whether a comment has any meaning outside of the post aggregate. IMHO, there isn't any, so I think you shouldn't move the comment to its own aggregate.