Note that there are some explanatory texts on larger screens.

plurals
  1. POShould Post and Comment be in the Post Aggregate or should they be aggregates of their own?
    primarykey
    data
    text
    <p>Consider the typical blog with objects <code>Post</code> and <code>Comment</code>.</p> <p>For a DDD demo example i have been building i have (till now) found that both the entities <code>Post</code> and <code>Comment</code> have been appropriate for the same aggregate- the <code>Post</code> aggregate. But now i'm not so sure..</p> <p>In my controllers i am finding, like you would expect, that i need to add and remove <code>Comments</code> from <code>Posts</code>. With my current model i am not tracking the identity of a <code>Comment</code> globally (like the Blue Book suggests). You you might expect that my action to delete a <code>Comment</code> may look like this:</p> <pre><code>public ActionResult DeleteComment(int postID, int commentID) </code></pre> <p>Obviously i need the <code>Post</code>'s id to retrieve it from the repository and the identifier for the particular <code>Comment</code> on that <code>Post</code> that i want to delete.</p> <p>My problem is the body of the <code>DeleteComment(</code> action:</p> <p>Is it ok to traverse the <code>Post</code> with a query mechanism to get the <code>Comment</code> for deletion? like this:</p> <pre><code>var comment = this._postRepo.WithID(postID).Comments .SingleOrDefault(c =&gt; c.ID == commentID); this._postRepo.Delete(comment); return RedirectToAction("detail", new { id = postID }); </code></pre> <p>..or should i be selecting the <code>Comment</code> from the repo similar to this?:</p> <pre><code>var comment = this._postRepo.CommentWithID(commentID) </code></pre> <p>..or:</p> <pre><code>var comment = this._postRepo.CommentWithID(postID, commentID) </code></pre> <p>The two above examples might seem a little silly since i shouldn't need the <code>Post</code> ID if i can track the <code>Comment</code> globally. But then if i'm tracking the <code>Comment</code> globally, shouldn't it have it's own aggregate and then is that right when <code>Post</code> and <code>Comment</code> seem to go together?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COI think that Post and Comment are different aggregates. You may want to query comments across all posts, for instance. This morning Ayende Rahien (RavenDB author) did a webinar on document databases with the Post/Comment model and he made them separate roots but had a denormalized query for displaying a single post with it's comments. Granted the RavenDB website shows a similar model with everything under Post. I suppose where you put it really depends on what you want to do with the model.
      singulars
    2. CO@Ryan Agreed. And i'm feeling that it will need to move to separate aggregates. I just want to make sure i'm doing the right thing. And when you say "it really depends on what you want to do with the model" - all im trying to do is the basic AddPost, UpdatePost, DeletePost, AddComment and DeleteComment at this stage - this demo is simplistic so i can understand the DDD decisions involved. "denormalized query for displaying a single post with it's comments": does this mean a way for the Post repo to return Posts and Comments together in an 'aggregated' fashion?
      singulars
    3. COYes it would. You could probably return a view model directly from your service/repo layer that has a post with it's associated comments.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload