CQRS and Mediator pattern
Tags: dotnet, cqrs, mediatr, restapi
Published: 2024-07-05 07:29:52

## CQRS- Command Query Responsibility Segregation
CQRS stands for **Command Query Responsibility Segregation**. Its a design pattern in software architecture that separates the operations that update data- **COMMANDS**, from the operations that read data - **QUERIES**.
This separation can improve performance, scalability, and security of an application.

•  **Commands**: Operations that change the state of the application. For example, creating a new user, updating an order, or deleting a record. Commands are often validated before execution and might involve complex business logic.
•  **Queries**: Operations that read data without modifying it. For example, fetching user details, retrieving a list of orders, or getting the status of a specific record. Queries are typically optimized for read performance and can be cached for faster retrieval.
•  **Segregation**: By separating commands from queries, you can optimize and scale each side independently. For instance, you might want to use different databases or models for reads and writes. This can also lead to a more secure system, as read and write permissions can be managed separately.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/93jtwhs3xhrqi4iqd7d0.png)


Benefits of CQRS:
•  **Scalability**: You can scale read and write operations independently based on their specific load and performance requirements.
•  **Optimized Performance**: By tailoring the data model for reading and writing separately, you can optimize each for its specific use case.
•  **Simplified Business Logic**: Commands can be handled in a way that ensures complex business rules are applied correctly, while queries can be optimized for fast data retrieval.
•  **Improved Security**: With clear segregation, you can apply different security measures to read and write operations.

CQRS is often used in conjunction with the Mediator pattern. This can be implemented using the MediatR library.

## Mediator Design Pattern - 

Mediator design pattern is used to handle communication between different components or objects in a system. It promotes loose coupling by ensuring that objects do not communicate directly with each other but instead through a **mediator**. This can simplify the dependencies between objects and make the system easier to maintain and extend.

Implementation of mediator pattern in .NET is popular through the use of the **MediatR** library. It provides a simple in-process messaging library that helps implement the mediator pattern. 

Key Components of MediatR -
•  **Requests**: These are the messages that are sent through the mediator. It can be a Command or a Query(explained above)
•  **Handlers**: These are the components that handle the requests. Each request has a corresponding handler that contains the logic to process the request. We usually have separate handlers for Commands and Queries.
•  **Mediator**: The mediator component routes the requests to the appropriate handlers. MediatR provides the IMediator interface to send requests and publish notifications.


Checkout the repository for understanding this through code - https://github.com/ShashankSaini203/RestApiPlayground

Feel free to reach out. Connect me at [LinkedIn](https://www.linkedin.com/in/shashanksaini203)