Should Post and Comment be in Post Aggregate or should they be their aggregates?

Consider a typical blog with Post and Comment objects.

For the demo DDD I created, I (so far) have found that both the Post and Comment objects were suitable for the same aggregate — the Post aggregate. But now I'm not sure.

In my controllers, I find, as expected, that I need to add and remove Comments from Posts . With my current model, I do not track Comment identification around the world (as the Blue Book suggests). You can expect my Comment removal action might look like this:

 public ActionResult DeleteComment(int postID, int commentID) 

Obviously, I need a Post ID to retrieve it from the repository and an identifier for a specific Comment on the Post that I want to delete.

My problem is the body of the DeleteComment( :

Is it possible to go through Post using the request mechanism to get Comment to be deleted? eg:

 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 choose Comment from a repo like this ?:

 var comment = this._postRepo.CommentWithID(commentID) 

.. or:

 var comment = this._postRepo.CommentWithID(postID, commentID) 

The two examples above may seem a little silly since I don't need a Post id if I can track Comment globally. But if I track Comment globally, shouldn't it have its own aggregate, and then is it right when Post and Comment seem to go together?

+4
source share
3 answers

The question is whether the comment has any meaning outside of message aggregation. IMHO, they are not, so I think that you should not move the comment to your own aggregate.

+1
source

In my opinion, the comment should be part of Post Aggregate, but the comment must be an entity, bcoz two comments with the same answer are still two separate comments.

If you create a comment as a separate unit, where comment is the root, then the comment will have a repository method, which means that any body can create a comment, but the main idea is that a comment should not be created without its post.Comment is associated with the message.

If you think logically, the comment cannot be changed. This means that when creating a comment, it must be part of the message.

+3
source

As others have said, it depends a lot on whether the comment has any meaning outside the Mail. I tend to think that this happens for several reasons. Firstly, things other than posts can be conceptually commented on in a regular blog (e.g., image, news, other comment). Secondly, as was shown, you often see widgets only comments that are not dependent on their posts. I also think that this scenario makes decisions that you torment more trivially.

However, if you decide to make them a single aggregate, remember that the repository often loads the entire aggregate when executing queries, relying on mechanisms such as caching, etc., to make it effective. Thus, your script will be a request for publication, followed by a search for these comments for comments “correct” for editing / deleting / independently.

+2
source

Source: https://habr.com/ru/post/1341256/


All Articles