Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQRS: Read model built on demand?

As I understand CQRS advocates separating read models from domain models and having a particular read model for every necessary domain model projection.

From usage point, how the read model is stored and retrieved should be transparent - you issue a query and get a read model without caring about how it is made.

Many examples and articles use separate tables for storing read models and re-generating them by response to domain model changes.

I do not really like this approach because of the following reasons:

  • Not all possible read models will be needed often;
  • Requirement changes might invalidate existing read models so all of them will need to be regenerated;
  • If for some reason read model contains properties that cannot be stored on generation but need to be calculated you are forced to use stored procedures/functions/views;
  • Since read models are separate from domain models in case application-level caching is used on domain model change you need to inform all applications that old read models need to be evicted from cache;
  • Sometimes it is not possible nor desirable to denormalize complex object graph fully therefore you need to have read models that are consistent for particular domain entity version, that is they need to be generated in same transaction;
  • Some domain entities have properties that change frequently but need to be included in every read model.

Based on this I am thinking of having query services should:

  • For read models that need to be generated frequently and/or are simple projections of domain entities: do not store them but produce them via ORM by querying domain model entities in database;
  • For read models that don't need to be generated frequently and are complicated domain entity projections generate them and store in database tables.

Also I see some people suggest storing read models as blobs. The problem with storing read models as blobs is that in case you will need to search on them you will need to extract properties for indexing and if you need full text search even you have to store them in a format that can be understood by full text tools.

As you can see, I will basically want to have read model that exists after query execution only and is not generated based on domain change events. Is this solution acceptable for CQRS? The reason I am looking into CQRS is to improve application architecture by separating cacheable view models from user action handling, have AJAX enabled web applications with asynchronous updates after user actions, and reduce the room for junior developers to produce unmaintainable code by placing business logic all over the place and even non-faithful implementation of CQRS to me seems like good step into right direction.

like image 345
Arunas Avatar asked Aug 31 '12 12:08

Arunas


People also ask

What is read model in CQRS?

A read model is a model specialized for reads, that is, queries. It takes events produced by the domain and uses them to build and maintain a model that is suitable for answering the client's queries. It can draw on events from different aggregates and even different types of aggregate.

What problem does CQRS solve?

CQRS is a popular architecture pattern because it addresses a common problem to most enterprise applications. Separating write behavior from read behavior, which the essence of the CQRS architectural pattern, provides stability and scalability to enterprise applications while also improving overall performance.

What is CQRS pattern in C#?

What is CQRS? CQRS stands for Command and Query Responsibility Segregation. It is a command query pattern and can separate the read and write operations for a data store. CQRS uses a command and query pattern in which the command functionality only updates or writes data and the query functionality returns the data.


1 Answers

CQRS does not require that the read model is stored in separate tables (or documents if you use a document database), although this often is a good approach and works well in combination with event sourcing. A read model can be backed by a database view or ORM query for instance.

This might be a good approach when introducing CQRS to parts of a legacy system.

The approach you're suggesting here has been suggested before, for instance see this post by Ayende; I think it's called "thin read model" or similar. I'd say go for it.

You might be interested in reading

  • Jimmy Bogard's article on myths in CQRS.
  • Ayende's "limit your abstractions" series of posts, starting here
like image 188
Marijn Avatar answered Sep 21 '22 16:09

Marijn