In this article, we will be creating an ASP.NET Core clean architecture template for your next ASP.NET Core project.
Why Clean Architecture?
Clean architecture is a concept proposed by Uncle Bob in his book Clean Architecture as a way of building highly flexible and maintainable software solutions.
The concept of Clean Architecture is based on the Dependency Rule which states that source code dependency can only point inwards towards the application core.
The Application Core (Use Cases and Entities), holds the high-level business rules of the software which are usually stable and don't change often. These are usually the functional requirements of the application. For instance, an online store application could have the following use cases and entities:
// Use Cases:
public class AddProductToCartByCustomer {
public AddProductToCartByCustomer(Products product) {}
}
public class AddProductToInventoryByAdministrator {
public AddProductToInventoryByAdminstrator(Products product) {}
}
// Entities:
public class Products () {
public int ProductId { get; set; }
public string ProductName { get; set; }
}
public class Orders () {
public int OrderId { get; set; }
public int CustomerId { get; set; }
public DateTime DateCreated { get; set; }
}
Uses Cases could be interfaces or services while Entities are usually business model classes that are persisted.
Clean Architecture Layers
According to the ASP.Net Core documentation, our source code can be structured as separate projects or layers with proper dependency flow from outwards towards our application core layer. The dependency flow is as represented below:
Application Core Types includes Interfaces, Services, DTO (Data Transfer Objects) and Entities (Business Models).
Infrastructure Types includes EF Core types (DbContext, Migration),
Data access implementation types (Repositories),
Infrastructure-specific services (for example, FileLogger or SmtpNotifier).
User Interface Types includes Controllers, Filters, Views, ViewModels, Startup
Tests Types includes Unit Tests, Integration Tests
To create our template, we first have to create a blank solution in visual studio or using the command line
dotnet new sln -o CleanArchitecture
This creates a CleanArchitecture.sln file inside CleanArchitecture folder.
Next we move into the folder by running the following command
cd CleanArchitecture
Inside the folder, we will create the relevant projects or layers of our application.
dotnet new classlib -o CleanArchitecture.Core
dotnet new classlib -o CleanArchitecture.Infrastructure
dotnet new mvc -o CleanArchitecture.Presentation
dotnet new xunit -o CleanArchitecture.Tests
If you noticed, I changed the names a bit, yes, you can absolutely customize the structure to fit your needs.
Next, we will add these projects to the solution CleanArchitecture.sln.
dotnet sln CleanArchitecture.sln add .\CleanArchitecture.Core\CleanArchitecture.Core.csproj .\CleanArchitecture.Infrastructure\CleanArchitecture.Infrastructure.csproj .\CleanArchitecture.Presentation\CleanArchitecture.Presentation.csproj .\CleanArchitecture.Tests\CleanArchitecture.Tests.csproj
We're done! π
All we need do now is to create the relevant folders inside the individual project folders. For instance, for CleanArchitecture.Core project we will create the Interface, Service, Entities and DTO folders and do the same for the other projects according to our needs and requirements.
Here's a link to the template - Clean Architecture Template.
Thanks for reading, let me know what you think below and happy coding!!!
Discussion (5)
First of all, thanks for sharing but It's not best practice and It's not helpful. It's just introduced post. If you shared some class and some examples, It would be good thing and best practice!
I think you can edit it and you can add some DTO, some application rules. Because many people don't know "what is the architecture, such as what is the application, what is the service". I think you must think about it.
Thanks for the suggestion Enis, I'll update the repository to include the relevant folders. And yes, you just gave me an idea for my next post! I'll create a mini-project from the template and break it down step by step so people can understand.
Your welcome, I will be waiting your post bro.
Cool, a beautiful way to explain clean architecture.
Thanks