Search code examples
c#domain-driven-designaggregateaggregateroot

Code examples for aggregates roots and aggregates


I am trying to understand how to use Aggregate roots and Aggregates, but I can't find any concrete information or examples on it.

For example I have the following three Entities:

  1. Survey
  2. QuestionGroup
  3. Question

A Question entity cannot exist without a Survey or QuestionGroup entity. All Questions belong to a QuestionGroup, so my understanding is

QuestionGroup is the Aggregate root of Question

A QuestionGroup also cannot exist without being part of a Survey, so

Survey is Aggregate root to QuestionGroup

It seems like the above is a case of nested Aggregate roots.

Q1. How do you actually create an Aggregate root and Aggregate in c#? What does that look like in code? Do you use inner classes or does the Aggregate root hold a reference? I can't find any good examples on this.

Q2. Taking it one step further how code a nested Aggregate root?

Thx!


Solution

  • Survey is the aggregate root.

    class Survey {
        public IEnumerable<QuestionGroup> QuestionGroups { get ... }
    }
    class QuestionGroup {
        public IEnumerable<Question> Questions { get ... }
    }
    class Question {
        ...
    }