CQRS and MediatR Pattern Implementation Using .NET Core 6 Web API
In this article, we are going to discuss the working of CQRS and MediatR patterns and step-by-step implementation using .NET Core 6 Web API.
Agenda
- Introduction of CQRS Pattern
- When to use CQRS
- MediatR
- Step-by-step Implementation
Prerequisites
- Visual Studio 2022
- SQL Server
- .NET Core 6
Introduction of CQRS Pattern
- CQRS stands for Command and Query Responsibility Segregation and uses to separate read(queries) and write(commands).
- In that, 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 a single data model to read and write data, which will work fine and perform CRUD operations easily. But, when the application becomes a vast in that case, our queries return different types of data as an object so that become hard to manage with different DTO objects. Also, the same model is used to perform a write operation. As a result, the model becomes complex.
- Also, when we use the same model for both reads and write operations the security is also hard to manage when the application is large and the entity might expose data in the wrong context due to the workload on the same model.
- CQRS helps to decouple operations and make the application more scalable and flexible on large scale.
When to use CQRS
- We can use Command Query Responsibility Segregation when the application is huge and access the same data in parallel. CQRS helps reduce merge conflicts while performing multiple operations with data.
- In DDD terminology, if the domain data model is complex and needs to perform many operations on priority like validations and executing some business logic so in that case, we need the consistency that we will by using CQRS.
MediatR
- MediatR pattern helps to reduce direct dependency between multiple objects and make them collaborative through MediatR.
- In .NET Core MediatR provides classes that help to communicate with multiple objects efficiently in a loosely coupled manner.
Step-by-step Implementation
Step 1
Create a new application
Step 2
Configure your application
Step 3
Provide additional information
Step 4
Project Structure
Step 5
Install the Following NuGet Packages
Step 6
Create a Student Details class inside the model folder
Step 7
Next, add DbContextClass inside the Data folder
Step 8
Create one student repository and a class related to that.
IStudentRepository
StudentRepository
Step 9
After that, add read queries
GetStudentListQuery
GetStudentByIdQuery
Step 10
Next, create different commands
CreateStudentCommand
UpdateStudentCommand
DeleteStudentCommand
Step 11
Now, add Query and Command Handlers
GetStudentListHandler
GetStudentByIdHandler
CreateStudentHandler
UpdateStudentHandler
DeleteStudentHandler
Step 12
Configure the database connection string inside the appsettings.json file
Step 13
Register a few services inside the program class
Step 14
Next, perform database migration and update commands
add-migration “initial”
update-database
Step 15
After that, create Students Controller and inject MediatR service inside that to send query and command
Step 16
Finally, run your application and access different endpoints using swagger UI.
GitHub URL
https://github.com/Jaydeep-007/CQRSAndMediatRDemo.git
Conclusion
Here we discussed the CQRS and MediatR Design Patterns and their purpose and benefits in large-scale applications and step-by-step implementation using .NET Core Web API.