Clean code architecture and CQRS pattern based web api using .NetCore
Tags: cqrs, csharp, dotnet
Published: 2021-03-04 20:31:49

This tutorial is part of [**Micro service architecture in .NetCore**](https://dev.to/silentrobi/micro-service-architecture-in-netcore-3od5)

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](https://blog.cleancoder.com/uncle-bob/images/2012-08-13-the-clean-architecture/CleanArchitecture.jpg) 
If you don't know about clean code architecture, there are some useful links you can visit.  
- [The Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html)
- [Common web application architectures](https://docs.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures).

# CQRS and the Mediator Pattern
## CQRS
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b30erfncpkv9gyl17685.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utmqu4by8j42own9shqd.jpg)

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.

{% youtube UB7l433oZkA %}

## 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](https://github.com/silentrobi/Blog.Service.BlogApi)