Command Query Responsibility Segregation (CQRS) is a pattern that separates the concerns of reading and writing data in an application. It provides a clear separation between commands that modify data (write operations) and queries that retrieve data (read operations). By using CQRS, we can achieve a more maintainable and scalable architecture.
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Join the session 5 minutes prior to the session start time. We start on
time and conclude on time!
Feedback
Make sure to submit a constructive feedback for all sessions as it is very
helpful for the presenter.
Silent Mode
Keep your mobile devices in silent mode, feel free to move out of session
in case you need to attend an urgent call.
Avoid Disturbance
Avoid unwanted chit chat during the session.
3. 1. Introduction to CQRS
2. Introduction to MediatR
3. Key component of CQRS
4. When to use CQRS
5. Advantages of CQRS
6. Demo
7. Conclusion
4.
5. Introduction to CQRS
CQRS Stand for Command and Query Responsibility Segregation and uses to separate the read
(queries) and write(Command).
Queries Perform read operation and Command perform writes operation like create, update,
delete and return data
As we know, in our application we mostly use single data model to read and write data, which will
work fine and perform CRUD operation easily but when application is vast in that case in that
case our queries return different types of data as an object so that it become hard to manage with
different DTO objects.
Also, the same model is used to perform a write operation. As result, the model becomes more
complex, Also, when we use same model for both read and write operation the security also hard
to manage when application is large, and entity might expose data in wrong context due to the
workload on the same model
CQRS helps to decouple operation and make the application more scalable and flexible on large
scale
6. Introduction to MediatR
MediatR pattern is helps us to manage dependencies between object by preventing them from
directly communicating with each other.
MediatR pattern helps to reduce direct dependency between multiple objects and make them
collaborative through MediatR
Instead, the communication happen through the MediatR and this helps to implement CQRS
Command and Queries are sent to MediatR that map them to their respective handlers.
.NET Core MediatR provides classes that helps to communicate with multiple objects efficiently in
loosely coupled manner
7. Key Component of CQRS in .NET
Commands: Represent actions that change the state. They are handled by command handlers.
Queries: Represent data retrieval without side effects. They are handled by queries handlers.
Domain Models: Define the business logic and rules.
Read models: Optimized for queries, can be denormalized for performance.
Write models: Focused on encapsulating business logic and state changes.
9. When to use CQRS
When application is huge and access the same data in parallel.
CQRS helps reduce the merge conflict while performing multiple operation with data
In DDD terminology, if domain data model is complex and need to perform many operation on
priority like validation and executing some business logic so in that case, we need consistency
When has a high demand for data consumption.
Performance of data read must be tuned separately from the performance of data writes,
especially when the number of read is much greater than the number of writes.
There is a need for a team to focus on the complex domain model that is part of write model,
while another team can focus on the read model and user interface.
10. Advantages of CQRS
Separation of concern – when read and write side are separated, the model become more
maintainable and flexible. The read model is typically simpler, while write model involves more
complex business logic.
Optimized data schemas – On read side, a schema can be optimized for queries and on write
side, a schema can be optimized for updates
Simpler queries – Application queries can avoid complex join by storing a materialized view in
the read database
Security – it's easier to ensure that only the right domain entities are performing writes on the
data
11.
12. Conclusion
We discussed the CQRS and MediatR with .NET services and CQRS are great architecture
pattern and very useful feature software architecture and their benefits in large scale applications
and implementation of CQRS and MediatR with .NET services.