DEV Community

Mohammad Abu Musa RABIUL
Mohammad Abu Musa RABIUL

Posted on • Updated on

Clean code architecture and CQRS pattern based web api using .NetCore

This tutorial is part of Micro service architecture in .NetCore

Blog Api – Clean Code Architecture​ is a solution template which is built with Loosely-Coupled and Onion Architecture. It contains other necessary integrations.

Clean Code Architecture

Clean Code Architecture or often called Onion Architecture is a layered architecture that makes your project easy to manage and test.
You can find the common pictures of this concept from visual diagram below.
Alt Text
If you don't know about clean code architecture, there are some useful links you can visit.

CQRS and the Mediator Pattern


CQRS stands for “Command Query Responsibility Segregation”. The main purpose of this pattern is to split responsibility of commands (saves) and queries (reads) into different models.

If we think about commonly used CRUD pattern, we think it as working on same data model to perform CRUD operations. For an example if we have a user model class, we usually performs the CRUD operations on that user model class. However CQRS would instead have us split these operations into two models, one for the queries (Read), and another for the commands (Create-Update-Delete).

Alt Text

Mediator pattern

Mediator pattern 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.

Alt Text

We can see in the image above, Controller Service sends a message to the Mediator, the Mediator then invokes one or multiple services to handle the message. There is no direct dependency between green and blue components.

MediatR library for CQRS and Mediator pattern

MediatR is a .Net library that is used to implement CQRS with mediator pattern. However this library has a limitation. It only works on 'in process' CQRS implementation. The term 'in process' means .NET library that manages interactions within classes on the same process. It is not an appropriate library to use if we wanted to separate the commands and queries across two systems. In that case, a better approach would be to a message broker such as Kafka or RabbitMq.

Setting up project

Project Setup

First off, let’s open Visual Studio and create a new ASP.NET Core Web Application, selecting API as the project type.

Project Dependencies

Let’s install a couple of packages via the Package Manager Console.
PM> install-package MediatR
PM> install-package MediatR.Extensions.Microsoft.DependencyInjection
PM> install-package FluentValidation.AspNetCore
PM> install-package FluentValidation.DependencyInjectionExtensions
PM> install-package Microsoft.AspNetCore.Mvc.Versioning
PM> install-package MongoDB.Driver
PM> install-package Swashbuckle.AspNetCore.Swagger
PM> install-package Swashbuckle.AspNetCore.SwaggerGen
PM> install-package Swashbuckle.AspNetCore.SwaggerGen
PM> install-package Swashbuckle.AspNetCore.SwaggerUI
PM> install-package System.Linq.Dynamic.Core
PM> install-package AutoMapper
PM> install-package AutoMapper.Extensions.Microsoft.DependencyInjection
PM> install-package Autofac
PM> install-package Autofac.Extensions.DependencyInjection

You can find GitHub link here

Top comments (4)

1304654 profile image

Hi. Thanks for share you knowledge. I have a question about the responses when it comes from query side. Why always do you map in the query response the dto with the same entity with the same parameters. What it's the reason, instead of reaturn the query result with the entity type . Thanks

silentrobi profile image
Mohammad Abu Musa RABIUL • Edited

The reason is that your entity might have more parameters and your client doesnt need all those parameters. So, you create a DTO of what parameters your client want. For an example, client wants only name and age of an user. However, your entity has more information than that like telno, address, designation etc. Therefore, in order to provide what client wants, you create your custom DTO that contains only name and user property.

1304654 profile image

I understand. But in the case of return the same properties. It should be better return the entity instead of "duplicate " the object contract? Thank for the response.

Thread Thread
silentrobi profile image
Mohammad Abu Musa RABIUL

You can do that if you need to return exact entity.