Thursday, April 21, 2022

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

 Interesting thing about this article is bring ing mediaR pattern into clean architecutre which can support CQRS or command and Query much effectively at the same time manage required layers supported by Cleanr architecutre. 

Easy and useful Pattern to make code clean and  structure. 


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

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

Sunday, April 10, 2022

Clean Architecture With .NET 6

 

Software design and architecture, generally refer to the foundation, structure, and organization of the solution. While designing a sustainable solution, we should always consider maintainability at the top. Maintainability means the solution should be well architected with a strong base. The solution should follow the best principles and design practices.

Clean architecture is one of the most used software development design patterns because of its sustainable characteristics.

In this article, I will brief on what a clean architecture is and design a solution in .NET 6 following this architecture. 

  • Brief about Clean Architecture
  • Design a clean architecture solution with .NET 6
  • ASP.NET Core Web API using Clean architecture
  • Implementing Entity framework in clean architecture with .NET 6

We can use the same approach to design clean architecture with ASP.NET core MVC, ASP.NET Core Razor Pages, web API or Win Forms.

Clean Architecture

Clean Architecture which is also known as Domain-Driven Design has evolved with considerable improvements in the last several years. Some of the architecture names used for clean architecture over the years are given below:

Above mentioned architectures have similar design principles that have the primary idea to keep the core business login and application domain at the center of the solution structure.

Therefore, the whole idea of this architecture is to allow the core part, which consists of complete business logic and application entities, adaptive and flexible enough to deal with changing technology and interfaces. Additionally, the core application remains the same and independent of presentation layers, infrastructures, and databases.

In these fast-paced technologies, JavaScript frameworks, web framework, database and external parts become absolute or upgraded, using this clean architecture you can replace these items with minimum effort. This architecture saves huge time because the core application business logic and entity are independent of framework, presentations, databases, and external parts. Subsequently, the architecture is sustainable and is loosely coupled core business logic and entity with presentation layer or framework.

Let’s start with solution design.

Prerequisites

  • .NET 6 SDK (https://dotnet.microsoft.com/download/dotnet/6.0)
  • Visual Studio 2022 or Visual Studio Code

Implementing Clean Architecture with .NET 6

In this section, we will have a complete solution following the principles of Clean Architecture in .NET 6.

To commence, let’s create a blank solution in Visual Studio. We will open Visual Studio and choose the project template as Blank Solution as shown below.

We will give a name to the solution. I will use a dummy name as “RijsatCleanArchtecture” for this article, however, we always give a proper name based on the project. Subsequently, we will provide the location of the solution.

We will add core parts of the solution: Core logic and Entity (Model). Let’s add class libraries for those by right click on the solution and add the project as shown below.

Select the class library template. You can simply search it by typing words as shown.

We will name the class library.

Next screen will give the option to choose the framework. We will select .NET 6.

Similarly, we will create Application and Infrastructure libraries and organize the project as portrayed below.

Let me explain a bit more about the above organization of the libraries. We are grouping the core part of the solution that contains Domain and Application. These two libraries are independent of external Databases, Services, and presentation (UI). Similarly, we can keep all the external agents like infrastructure, Persistence, Drivers, Services into a separate folder Infra.

Now we will create a host project. We can again group host projects together. Host projects are generally presentation layers that contain WebApi, ASP.NET MVC, Razor Pages or Separate UI with Frontend Framework.

For this article, I will be using ASP.NET Core Web API as host or UI. Let’s create the host project in the solution.

We will provide a name for the Web API, and then we will get a few more options as shown.

There are options to choose framework, Authentication type, Https configuration, Docker Enable, Use controllers, OpenAPI support. You can choose the option based on needs.

However, we can add ASP.NET MVC or Razor Pages as a presentation UI based on our requirement. Importantly, the core (Business logic) and infrastructure part will be the same as the basic principle of clean architecture. 

Now, the solution will be organized as shown.

Now, we will add project dependencies. As per clean architecture principles, we keep the domain intact, which means there will be not any project reference or external package in this domain library. Next layer, application (business logic) will refer only to a domain library and use packages as tools only. 

Both UI and infrastructure will add an application library as reference. 

The project references will be as shown below.

  • There will be not any reference to Domain Library
  • Application: Add reference of Domain project
  • Infrastructure: Add reference of application project
  • WebApi: Add reference of application and Infrastructure projects

If you observe the above solution, the core business logic (Domain + Application) is independent of UI, database, external agent, and Infrastructure. However, UI and Infrastructure are dependent on core business logic. Therefore, if we change the UI, database or external services, the core part (Domain and Application) will remain the same, which makes the solution sustainable and maintainable in the long run.

Now, our solution is ready with clean architecture in .NET 6.

This article is a part of Clean Architecture with .NET 6 where I have designed a solution using the principles. In the next article, I will implement Entity Framework with the same solution.

Conclusion

To sum up, this article has explained what clean architecture is and design principles. Additionally, I have designed a clean architecture with .NET 6. I have also created ASP.NET Core Web API using the clean architecture standards. In the next article, I will implement entity framework in clean architecture with .NET 6 and do a CRUD operation.

React Profiler: A Step by step guide to measuring app performance

  As react applications are becoming more and more complex, measuring application performance becomes a necessary task for developers. React...