DEV Community


Clean Architecture In ASP.NET Core

joxiah profile image Eikhiena Blessed Josiah ・3 min read

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.

Clean Architecture

Robert C. Martin Clean Architecture

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:


Clean Architecture Dependency

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)

Editor guide
eniskurtayyilmaz profile image
Enis Kurtay YILMAZ

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.

joxiah profile image
Eikhiena Blessed Josiah Author

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.

eniskurtayyilmaz profile image
Enis Kurtay YILMAZ

Your welcome, I will be waiting your post bro.

mojemoron profile image
Micheal Ojemoron

Cool, a beautiful way to explain clean architecture.

joxiah profile image