Search code examples
c#design-patternsdomain-driven-designrepository-patternddd-repositories

What is the correct aggregate root?


I have the following data structure:

class Post
{
    public string Title { get; set;}
    public Category Category { get; set;}
}

Bearing in mind a post always belongs to one and only one category is my reasoning correct?

  • Category is the aggregate root (since a post cannot exist without it)
  • There should be a CategoryRepository (with methods such as GetCategory and GetPost) but not a PostRepository (because Post is not an aggregate root)

Solution

  • How do you want to reference instances of Post? Is Post.Title a unique identifier for a Post? If so, then Post is a valid aggregate root and you should create a PostRepository which retrieves an instance of Post given it's Title.

    Take the example of a car. A car must have a colour, but stating that the colour is the aggregate root just because a car cannot exist without one is the wrong thing to do. I want to reference a car independently given it's license plate number (which is independent of it's colour). The fact that it must have a colour is simply a feature of my car domain model which states that I cannot construct a car instance without supplying the colour.