I'm learning DDD and wanted to start modelling a fairly trivial example, a blog application.
One area开发者_StackOverflow中文版 I'm a little confused about is defining aggregate roots.
Here's my model so far:
Site
has many
Blog
has reference to Site
has many
Post
has reference to Blog
has Category
has many
Comments
In this example the only thing I am sure of is that Comment is a value object since it makes no sense outside the context of a post.
But then based on:
Only Aggregate Roots can be obtained directly with database queries. Everything else must be done through traversal.
I would be inclined to make Site, Blog and Post ARs since I would want to obtain them all directly rather than traversing a collection.
I realize that a Blog isn't exactly a complex domain model so doesn't really warrant applying DDD but I just want to understand how these type of relationships are modelled.
It is not because you have a hierarchy that you must define an aggregate. The aggregate construct will come handy if you have invariant to maintain within the hierarchy.
In your example, assume that you want each post in a blog to have a unique 'slug' (Wordpress style, so that the title appears in the URL). Only the 'Blog' entity can enforce this invariant. Similarly, in that case it would make sense to make it the aggregate and have the post added via the blog entity.
Another example: your site can be public or private. This is an attribute of the Site entity. You must therefore make sure to access to a post using traversal from the aggregate root which would be the site since only the site can authorize you or not to access the underlying objects.
If you make the site the aggregate root, you could end up with an url like this:
http://site.com/myblog/apost
Your code would first retrieve the site entity, from that entity it gets the blog, and from the blog entity it fetch the post. How you go about retrieveing the child entities is up to you. This model does not force you to load all blog posts in memory when retrieving the site. It just force you to retrieve the blog from the site, and the posts from the blog.
精彩评论