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:
Based on this I am thinking of having query services should:
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.
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.
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? 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With