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 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:
- Hexagonal Architecture (https://en.wikipedia.org/wiki/Hexagonal_architecture_(software))
- Onion Architecture
- Domain-Driven Design (DDD) or Domain Centric Architecture
- Vertical Slice Architecture
- Clean Architecture
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.
Let’s start with solution design.
- .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.
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.