Domain driven design (a sample blog application)

domain-driven-design

Solution

While the model you present reflects the domain, it isn't an optimal DDD implementation. With DDD, in addition to considering relationships between entities, you must also consider transactional consistency boundaries. As a result, it would be better to design a `Blog`, `Post` and `User` as separate aggregates which reference each other by ID only. Moreover, there is no reason that the `Blog` entity needs to have a collections of posts. You will never need to load the entire blog and behaviors never span multiple posts. Instead, provide a paginated repository method to load a subset of posts for a blog. A `Comment` however can be a value object and so the comments collection should be loaded together with the `Post` aggregate. The easiest way to get all comments for a user is to create a repository query method which returns a read-model to prevent conflating queries with behavior in your entities. For more on designing aggregates take a look at Effective Aggregate Design.

Problem

I was learning about DDD recently and didn't quite understand the concepts. I have some questions about a sample blog application. Let's assume that there are four domain objects in the blog system: `User`, `Blog`, `Post` and `Comment`. One `User` can have only one `Blog`, a `Blog` has multiple `Post` entities and a `Post` has many `Comment` entities. My design is that `Blog` is the aggregate root: ``` class Blog { private User; private List<Post> posts; } class Post { private List<Comment> comments; } class BlogRepository { public void saveBlog(Blog blog); public void findBlogById(long id); public void getAllBlogs(); } ``` Am I right to design the aggregate root and repository like this? I have some requirements to get all the `Comment` entities added by an user for all `Blog` entities, and also the `User` is allowed to modify her/his own `Comment`. My question is how can I implement these requirements?

Original source