Sergey Metlov
Sergey Metlov

Reputation: 26311

CQRS aggregated view

There is a common recommendation to have single table (or SQL view) per ViewModel. I can't really understand how to achieve this in the scenario then I need to show an aggregated model on the view and a list of children. Example:

Topic title

Topic description.

There is an aggregated Topic model that is supposed to be a root model and represented by the 1st SQL view and a number of child models - Comment linked to Topic with a foreign key. So we have 2 tables or views and join to show them on the screen.
And the questions are:

  1. Is it okay to have more than one joined tables for the single ViewModel?

  2. Is it possible to flatten the example above to keep it in the single SQL view? If so then how the ViewModel class should look like?

  3. What to do if we have more than 1 linked models?

Programming language doesn't really matter.

Upvotes: 2

Views: 142

Answers (2)

Alex Shkor
Alex Shkor

Reputation: 1209

A document oriented database better fits for Read model. You can store document per ViewModel, and there is no needs in joins, etc. But your ViewModel is not obligatory referred to only one document. In your example, if you load more comments by ajax, you can store comment in another document/collection/table. If you use search queries for comments, you also can store it in another document/collection/table. If you load all comments together with topic you can store it in one document (topic and comments together). Later, if you decide to load comments by ajax or allow user query comment, you can split this document or store comments in another collection/table additionally.

Upvotes: 1

Martijn van den Broek
Martijn van den Broek

Reputation: 1381

The recommendation is to store your data in a format that suits your needs. Joinless queries are preferred, not mandatory. A practical alternative would be to store your views as documents in a document database.

Upvotes: 0

Related Questions