Building an Onion Architecture Solution with Dotnet CLI
In this article, we will briefly answer the question of “How we can create a project using onion architecture via dotnet cli?”
We use architectures like the Onion architecture to clean code and build extensible code structure because we want to have a clean code and a clean architecture, that is, a structure that is loosely coupled and does not resist change.
So, we divide the application into layers and minimize the dependency between these layers and obtain a loosely coupled structure.
Structure of Onion Architecture
The onion architecture can be realized in different ways, basically it is based on having a domain in the core and the outer layers referencing the layers close to the core. In this article, we will build a structure like this;
1. Core
- Application
It is the layer where the abstraction operations of the project are performed, it is located in the core of the project.
- Domain
It covers the basic domain related (e.g. Entities) areas of the project, located in the project core.
2. Infrastructer
- Persistence
Database related processes are located here. Thus, unlike the traditional tiered architecture, the domain, not the database processes, is in the core.
- Infrastructer
Where service implementations of external resource access are done.
3. Presentation
The presentation layer is the layer where the project is presented to the user, in this article we will present using an api.
In short, there are many projects that we will create as a library (classlib) to create the onion architecture structure we mentioned with dotnet cli, and there are also projects that will refer each other, whether it is close to the center of the onion or outside, it is all about this referencing, that is, being dependent.
Let’s name the project Cleany.
mkdir Cleany && cd Cleany
dotnet new sln
dotnet sln Cleany.sln list
The project folder and the sln file is created, list command to read the projects included in the sln file.
Core/Domain Layer
dotnet new classlib -o Core/Domain
dotnet sln Cleany.sln add Core/Domain
dotnet sln Cleany.sln list
Core/Application Layer
dotnet new classlib -o Core/Application
&& dotnet sln Cleany.sln add Core/Application
Unlike the traditional layered architecture, the Application layer is the layer that refers to the Domain Layer, not the Data Access Layer. Thus there is less dependency on Data Access Operations and hence loose coupling. The loose coupling in the Onion architecture is due to the fact that interfaces are created in the application layer, i.e. the abstraction is done here.
Infrastructure Layer
dotnet new classlib -o Infrastructure/Infrastructure
&& dotnet sln Cleany.sln add Infrastructure/Infrastructure
&& rm Infrastructure/Infrastructure/Class1.cs
The last of the commands, delete file, is to delete the file named Class1.cs that comes by default when classlib is created via dotnet cli.
Persistence Layer
dotnet new classlib -o Infrastructure/Persistence
&& dotnet sln Cleany.sln add Infrastructure/Persistence
&& rm Infrastructure/Persistence/Class1.cs
Presentation Layer / Web API
dotnet new webapi -o Presentation/API
&& dotnet sln Cleany.sln add Presentation/API
When all these operations are done and sln projects are listed, the following image is created.
dotnet sln Cleany.sln list
Project(s)
----------
Core/Domain/Domain.csproj
Core/Application/Application.csproj
Infrastructure/Infrastructure/Infrastructure.csproj
Presentation/API/API.csproj
Reference addition operations
dotnet add Core/Application/Application.csproj
reference Core/Domain/Domain.csproj
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
Now when we come to the project file of the Application Layer, that is, when we come to Application.csproj, when we read inside, when we open it, we see that the domain has been added here, so that our enterprice assets will be available in the application layer.
dotnet add Infrastructure/Infrastructure/Infrastructure.csproj
reference Core/Application/Application.csproj
dotnet add Infrastructure/Persistence/Persistence.csproj
reference Core/Application/Application.csproj
Since the Application layer also references the domain layer, we also access the domain, at this point, let’s reference the infrastructure layers for the Presentation Layer.
dotnet add Presentation/API/API.csproj
reference Infrastructure/Infrastructure/Infrastructure.csproj
dotnet add Presentation/API/API.csproj
reference Infrastructure/Persistence/Persistence.csproj
We talked about how we can easily set up the solution structure without using Visual Studio and how we can do this using the terminal, and as an example, we realized the onion architecture.
Thanks for reading