In this article let’s us learn the basics of CQRS pattern using MediatR nuget package in .Net 5.
We know how a traditional web API works. It mainly consists of the CRUD operations. All the four operations are tied together inside an API controller.
- C – Create
- R - Read
- U – Update
- D – Delete.
To decouple the application we create interfaces for our data access layer, inject it in the constructor of API controller and perform the actions. This works fine and all looks good. But as your application grow in size the number of dependencies also increases. Now we need to inject multiple interfaces in the API controller and hence the application complexity increases.
In order to solve this problem, let’s use CQRS & Mediator Patterns.
CQRS stands for Command Query Responsibility Segregation. That is separating Command (write) and Query (read) models of an application to scale read and write operations of an application independently. Instead of having all of the four CRUD operations together, let us segment out to two different pieces.

Mediator is used to reduce communication complexity between multiple objects or classes. This pattern provides a mediator class which normally handles all the communications between different classes and supports easy maintenance of the code by loose coupling. Mediator pattern falls under behavioural pattern category.
Basically, a Mediator perform 2 operations.
- · Accept the incoming request
- · Handles the incoming request and provide the response.
CQRS + Mediator Patterns are prefered over large projects.
Let’s get into the code and see how this CQRS & Mediator patterns work together.
Read data using CQRS & MediatR
Step 1
Open Visual studio 2019. Create an ASP.Net Core Web API project named EmployeeManagement.API. I have given the solution name as EmployeeManagementDemo. (You can give any name of your choice).

Step 2
Make sure that you select the .Net 5.0 as target framework and enable the Open API support for Swagger and click create.

Step 3
Add a new class library project named EmployeeManagementLibrary. Select the target framework as .Net 5.0


Step 4
Delete the class1.cs file.
Step 5
Add folders named Data & Models. In the Models folder, add a class named EmployeeModel with three properties named Id, FirstName & LastName.

Step 6
Create a class named DataAccess and an interface named IDataAccess in the Data folder. In this example I am using an in-memory collection for storing data. If you want to try it out with any DB, you may add the logic to perform DB operations.
Step 7
Add two methods named GetEmployees & AddEmployees to the IDataAccess interface as shown in figure below.

Step 8
Let DataAccess class inherit from IDataAccess interface and implement the interface as shown in figure below.

Step 9
Now let’s add the MediatR nuget package to EmployeeManagementLibrary project. Browse for MediatR nuget package in install it.
At the time of writing this article the latest stable version of MediatR is 9.0.0.

Step 10
Add two new folders named Queries & Handlers in EmployeeManagementLibrary project.
Step 11
Add a class named GetEmployeeListQuery in the Queries folder. I am using records (C# 9 feature) instead of a class. If you are not familiar with records, I highly recommend you to go through it. Let us implement the IRequest interface of MediatR package as shown in figure below.

IRequest is a generic interface. We need to specify the return type of the request. In our case I have specified List<EmployeeModel> as I need to return all employees.
Step 12
We need to handle this request. For that let us add a handler class in Handlers folder. In our case I have added a class named GetEmployeeListHandler.
Step 13
GetEmployeeListHandler implements the IRequestHandler interface of MediatR package as shown in figure below.

IRequestHandler is a generic interface. So what do you mean by IRequestHandler<GetEmployeeListQuery,List<EmployeeModel>? It means that if the request is of type GetEmployeeListQuery, then return List<EmployeeModel>.
Step 14


















Ed ZedPosted Jan 9, 2022, 8:54 PM
This is Tim Corey's video course https://www.youtube.com/watch?v=yozD5Tnd8nw
상현 김Posted Dec 14, 2021, 4:53 PM
What do you think about this? https://github.com/sang-hyeon/Plastic
Efim ZabarskyPosted Nov 4, 2021, 7:52 AM
Thanks, Praveen
Nigel MaddocksPosted May 28, 2021, 3:54 PM
Good article. A couple of corrections: - 1. In order to actually add into the database requires _employees.Add(newEmployee); in AddEmployee method - 2. To retain changes, change AddScoped to AddSingleton within Startup.ConfigureServices
Cory MelendezPosted Apr 12, 2021, 3:20 AM
This looks surprisingly like Tim Corey's video on the same subject:https://www.youtube.com/watch?v=yozD5Tnd8nw&t=2217s just with the term 'Person' replaced with 'Employee'