In this tutorial, we are going to cover the CQRS(Command Query Responsibility Segregation) pattern in Asp.Net Core 3.1 web api application. We will see how to implement CQRS in a very basic CRUD application using the MediatR library. MediatR library is an open source implementation of mediator pattern for .NET Applications.
So, we will create a very simple CRUD application in Asp.Net Core using CQRS Pattern and Mediator Pattern. We will cover these below steps in this tutorial.
- What is cqrs pattern?
- How to install MediatR Library in Asp.Net Core application?
- How to implement cqrs pattern in our application?
- How to create a controller using the mediator pattern?
- We will test our application using Postman.
Note: – In the previous tutorial, we have covered how to integrate react and asp.net core 3.1 application using vs code. So, we will not create a new web API project in this tutorial, we will use the Asp.Net Core web API project which we have created in the previous tutorial.
How to implement the CQRS Pattern and the Mediator Pattern in Asp.Net Core 3.1 web API application.
Let’s start how to implement the cqrs pattern and Mediator pattern in Asp.Net Core 3.1 CRUD application using a step by step example.
1- what is CQS?
CQS means command query Separation. It allows separate read and write operations. Commands help us to write the database and the queries help us to read the database. We need handlers for Command and Queries. And the mediator pattern allows us to implement Command/Queries and Handlers loosely coupled.
In simple terms, first we need to define a request and the request describes the behavior of commands and queries. And if the request is created, then we need a handler to handle the request.
Note: – we will implement this approach in section 3.
2- How to install the MediatR library in Asp.Net Core 3.1 application?
Now, in this step, we will install the MediatR library in Asp.Net Core 3.1 project. So, press ctrl+shift+p and it will show a search box on top. Now, search for NuGet package and then press enter as you do see below in the screenshot.
Now, write the MediatR in the search box and then press enter and then you will see the packages. Now, select the MediatR.Extensions.Microsoft.DependencyInjection package and then press enter as you do see below in the screenshot.
Note: – make sure you have selected the right package as you do see the above screenshot.
Now, select the latest version and then select the project where you want to install the package as you do see below in the screenshot.
Now, click on the Restore button from the right bottom and it will restore all the packages.
3- How to implement the CQRS in Asp.Net Core 3.1 application?
Now, in this step, we will see how to implement the CQRS pattern in Asp.Net Core 3.1 application. I have created a web API project in the previous tutorial which has a Book domain class as you do see below in the screenshot.
And i have created a DbContext class and created a database in the previous tutorial. If you want to know how to do it, then go to the previous tutorial.
So, now create a new folder with the name of bookM (M for Mediator) within the API project as you do see below in the screenshot. We will create all the handlers within this folder.
List Query Handler:
Now, we are going to create our first handler and this handler will bring back the list of books. So, right click on the bookM folder and then create a new class with the name of List and then write the code as you do see below in the “List.cs” file.
Let’s understand the above file’s code.
Line # 13: here in this line we are creating a Query class which is inherited from IRequest Mediator Interface. And this interface will return the List of books to this Query.
Line # 15: here in this line, we are creating the handler for the query. And this handler is inherited from the IRequestHandler with two parameters. One is query class and the other one is what we want to return from this handler. In our case, we are returning a List of books.
Line # 22: here in this line, we are implementing the IRequestHandler interface and it will create a Handle method within the Handler class.
Line # 24: Here in this line, we are getting the list of books from the database.
Detail Query Handler:
Now, we are going to create a Detail handler. Which will return the specific book by its id. So, again right click on the bookM folder and then create a new class with the name of Detail and then write the code as you do see below in the file.
Let’s understand the above code.
Line # 12: here in this line we are creating a Query class which is inherited from IRequest Mediator Interface. And this interface will return the single book to this Query. We have one Id property in this Query class to get a specific book from the database.
Line # 17 to 29: here in this block of code we are getting a specific book from the database. And this handler is inherited from the IRequestHandler with two parameters.
Create Command Handler:
Now, we are going to create a “Create” handler and it will insert the book into the database. This is our first command handler. So, create a new class with the name of “Create” within the bookM folder and then write the code as you do see below in the file.
Let’s understand the above code.
Line # 13: Here in this line, we are creating the Command class which is inherited from IRequest interface from MediatR. And this command class has some properties same as the Book class.
Line # 22: Here in this line, we are creating the Handler class which is inherited from the IRequestHandler interface.
Line # 31 to 38: Here in this block of code, we are initializing the book object and then inserting into the database. And line 38 will return the value. If it will return the value greater than 0, it means the object is created successfully. Otherwise, it will return the exception.
Edit Command Handler:
Now, we are going to create a “Edit” handler and it will update the specific book object into the database. This is also the command handler. So, create a new class with the name of “Edit” within the bookM folder and then write the code as you do see below in the file.
Note: – this handler is almost the same as the Create Command Handler. If you have any query then please ask in the comment section.
Delete Command Handler:
Now, we are going to create a “Delete” handler and it will delete the specific book object into the database. So, create a new class with the name of “Delete” within the bookM folder and then write the code as you do see below in the file.
So, we have created all the handlers that we need in our CRUD application.
Add mediator Service in the startup class
Now, we need to add a mediator service within the startup class. So, go to startup class and then write the below highlighted line of code within the ConfigureServices method as you do see below in the screenshot.
Note: – don’t forget to add MediatR library reference.
Here in the above highlighted line of code, we are passing the assembly reference in the mediator where all the handlers are located.
Let’s move to the next part.
4- How to create a controller using the mediator pattern?
In this part, we will see how to create a Books controller. So, go to the Controllers folder within the API Project folder. Now, right click on the Controllers’ folder and then create a new controller with the name of “BooksController.cs” and then write the code as you do see below in the file.
Let’s understand the above code.
Line # 14 to 18: Here in this block, we are injecting the IMediator.
Line # 21 to 24: Here in this block, we are getting the list of books. And in line # 23, we are sending the message to the List query handler using the mediator to get the list of books.
Line # 27 to 30: Here in this block, we are getting the specific book by its id. And the line # 29, is sending the message to the Detail query handler with specific book id.
Line # 33 to 36: Here in this block, we are sending the command to the Create Command Handler to insert the object into the database.
Line # 39 to 43: Here in this line, we are sending the command to the Edit Command handler to update the specific book object using the specific book id.
Line # 45 to 48: Here in this block, we are sending the command to the Delete Command Handler to delete the object using the specific book object id.
Now, we have successfully completed the controller part of our application. Let’s move to the next part.
5- Test our application using Postman
Note: – If you don’t have the Postman, then click here to download.
Now, in this part we will test our project using Postman. So, run the API project and then go to postman and then enter the url (http://localhost:5000/api/books) and then set the method type as Get and then press the Send button. Then you will see the output as you do see below in the screenshot.
Now, we will get the specific book by id. So, enter the URL (http://localhost:5000/api/books/1) and then press the Send button and then you will see the output as you do see below in the screenshot.
Now, enter the URL (http://localhost:5000/api/books) and then set the method type as POST and then enter the object in the JSON format in the body section and then press the Send button. It will insert the record into the database as you do see below in the screenshot.
So, in the same way we can update the record using postman. Congratulations, we have successfully implemented the CQRS pattern using the MediatR in Asp.Net Core 3.1 application.
If you have any query, then please ask in the comment section. Don’t’ forget to like and share within your community.
Leave a Reply