Search code examples
c#domain-driven-designaggregaterootinvariants

What are read-models within Aggregates in DDD and where to use them?


I know in DDD that deleting an Aggregate root would mean removing everything within the Aggregate boundary at once.

But I have noticed that read-models(read-only properties) are used within aggregates as Lev Gorodinski stated in this blog: http://gorodinski.com/blog/2012/04/25/read-models-as-a-tactical-pattern-in-domain-driven-design-ddd/

But as those models are part of the aggregate.So,how we treat them in case of deleting or updating our aggregate?

Also,can we use them to ensure the invariants/consistency of Aggregates?

I would appreciate an example about them and how we use them within aggregates without violating DDD principles.


Solution

  • But as those models are part of the aggregate.So,how we treat them in case of deleting or updating our aggregate?

    They are not part of the Aggregate (the Aggregate is the write model). In best case they are a view or a projection of one or more Aggregates.

    Also,can we use them to ensure the invariants/consistency of Aggregates?

    No because they are not part of the Aggregate. You could use them for validation but in an eventually consistent pre-aggregate validation and in very special cases (if you need this validation to be strong consistent then you should review your Aggregates boundary as they are probably wrong)

    P.S. That blog post states that these read models are to be use by the UI or similar and not by the Aggregates.