ASP .NET CORE Web API With CQRS (Command Query Responsibility Segregation) + MediatR =❤️

Ebrar Tepeciklioğlu
5 min readSep 17, 2023

Hi🙂 First of all, let’s start by examining what CQRS and MediatR are, what they are useful for, where we should use them, and what we can make better with the CQRS architecture.

CQRS

Command Query Responsibility Segregation is an abbreviation. It is a software architecture and aims to optimize application operations by separating command and query responsibilities.

CQRS

As can be understood from the meaning of CQRS, this architecture aims to separate commands and queries from each other. While the Create, Read, Update and Delete operations we use in our software projects are separated as Commands, our query operations correspond to the Query section.

This architecture is especially useful in large-scale applications. Because application performance can be increased by distributing the load of database operations more evenly. CQRS also facilitates parallel development of different components of the application. However, implementation of CQRS presents some challenges and therefore its use should be carefully considered.

This architecture is divided into 2 different models:

Queries: It only returns data and does not change the state of any object or system.

Commands: Give, good or change the state of the system.

Now let’s examine this structure in more detail.✌️😇

Commands

It refers to changes in the data source. It is done through operations such as adding, deleting and updating the data. It is not expected to return an answer, this is a job that the Query must do.

If there are rules of the project we are developing or conditional controls that we need to comply with, these are done at this stage.

Query

It is responsible for retrieving the data from the data source and should not change any values of the data.

Query usually starts with the Get keyword in the naming standard. In the following parts of the article, the query expression will be called reading.

Advantages of CQRS 🎉

  1. Low dependency
  2. Scaling
  3. Ability to separate tasks
  4. Ability to distinguish domains
  5. Performance scalability
  6. Ability to use for different databases

Disadvantages🤔

  1. Code complexity
  2. Error handling

When to use CQRS?😇

  1. It can be used in large and multi-user systems, where complex and always changing business rules exist.
  2. With CQRS, great performances can be achieved on read and write operations, and these operations can be optimized and divided into different databases if desired.
  3. CQRS allows us to get rid of complex domain logic operations in infrastructural operations.
  4. CQRS also allows us to divide the development stages into different teams, so that each team can work on domain logic.

Before moving on to our project, I would like to talk a little about MediatR.😇

MediatR

It is a library that allows the use of the Mediator pattern.

Mediator is a behavioral design pattern that helps reduce chaotic dependencies between objects. This pattern limits direct communication between objects and forces communication only through the mediator object.

Let’s get into practice! ✌️😇

First of all, we create a .Net Core Web API project. We choose Net6.0 .

Let’s install the MediatR library and its dependencies.

install-Package MediatR
install-package MediatR.Extensions.Microsoft.DependencyInjection

Let’s add MediatR to the program class:

builder.Services.AddMediatR(typeof(Program));

After adding the relevant folders to our project, its structure will be as follows.

I preferred to create a FakeData class within the project and see addition and deletion operations this way.

FakeDataStore.cs

For example, I created our GetProductsQuery, GetProductByIdQuery and AddProductCommand classes and the AddProductHandler, CacheInvalidationHandler, EmailHandler, GetProductByIdHandler and GetProductsHandler classes that handle these classes as follows.

AddProductCommand.cs
GetProductByIdQuery.cs
GetProductsQuery.cs

The classes I created to create handlers are as follows.

AddProductHandler.cs
CacheInvalidationHandler.cs
EmailHandler.cs
GetProductByIdHandler.cs
GetProductsHandler.cs

When the endpoints in the ProductsController receive a request, MediatR sends the request to the handler written for the relevant method with the help of Send.

ProductController.cs

Handler is defined with GetProductByIdHandler, IRequestHandler. While Send is managing here, it looks at our request, GetProductByIdQuery, and decides which handler it should direct to.

Thus, since there is no transactional dependency in systems where transactions are carried out via Event, reading and writing actions do not wait for each other. For this reason, CQRS infrastructure is used where performance is important.

Also, I encountered such an error while compiling the project.Also, I encountered such an error while compiling the project.🙂

 builder.Services.AddMediatR(cfg => {
cfg.RegisterServicesFromAssembly(typeof(Program).Assembly);
});

Software development with CQRS is a costly method. If used in the right place, the developed system will be easier to maintain and maintain. For this reason, as in every software development pattern, it should be used where necessary.

I shared the project for you on my github address. You can download and try it if you wish.🥳

Project : https://github.com/ebrartep/MicroFrontend.git

In my next article, I will try to show step by step how to connect with the database and start a more complex project. 🙂

See you in my next article. Lots of coding!👋❤️

--

--