Clean Architecture in ASP .NET Core Web API

Mohaned Zekry
4 min readApr 28, 2024

--

Mastering Scalability: Crafting a Maintainable Web API with ASP.NET Core and MediatR

What is Clean Architecture?

Clean Architecture is a design pattern that separates an application into different layers based on their responsibility. It’s a way of organizing your code into independent, testable, and reusable components. This architecture pattern is a software design methodology that emphasizes the separation of concerns and separates the application into distinct modules.

The primary objective of Clean Architecture is to create a structure that makes it easy to manage and maintain an application as it grows and changes over time. It also makes it easy to add new features, fix bugs, and make changes to existing functionality without affecting the rest of the application.

Implementing Clean Architecture in ASP .NET Core Web API

To apply Clean Architecture, we can divide the application into four primary layers:

  1. Domain Layer → the project that contains the domain layer, including the entities, value objects, and domain services
  2. Application Layer → the project that contains the application layer and implements the application services, DTOs (data transfer objects), and mappers. It should reference the Domain project.
  3. Infrastructure Layer → The project contains the infrastructure layer, including the implementation of data access, logging, email, and other communication mechanisms. It should reference the Application project.
  4. Presentation Layer → The main project contains the presentation layer and implements the ASP.NET Core web API. It should reference the Application and Infrastructure projects.

Project Structure

The project structure for a Clean Architecture in ASP.NET Core Web API:

Domain Layer

The domain layer represents the application’s core, encapsulating business rules, entities, and domain-specific logic. It should be technology-agnostic and contain no dependencies on external frameworks or libraries.

There are two folders inside the Domain project: Common and Entities.

Common
The Common folder is used to store BaseEntity class and other aggregates:

 public class BaseEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.Now;
public DateTimeOffset? DateUpdated { get; set; }
public DateTimeOffset? DateDeleted { get; set; }
public Guid UserID { get; set; }
public bool IsDeleted { get; set; } = false;
}

Domain
All domain entities are stored inside the Entities folder:

 public class TestTakers : BaseEntity
{
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FormNumber { get; set; }
public string BannerID { get; set; }
}

Application Layer

The application layer contains the business logic and use cases of the application. It acts as an intermediary between the presentation layer and the domain layer. This layer is independent of any specific UI or infrastructure concerns.

Features
The Features folder inside the application layer is a standard convention in Clean Architecture that provides a way to organize the application code by functional feature. This makes it easier to understand the application’s overall structure and maintain the codebase over time.

Infrastructure Layer

The infrastructure layer deals with external concerns such as databases, external services, and frameworks. It contains implementations of interfaces defined in the application layer and interacts with external resources.

Presentation Layer

This layer is responsible for handling user interactions and delivering data to the user interface. In a .NET Core Web API, this layer comprises the controllers and other components that handle HTTP requests and responses.

The source code of this article can be found here:

Thanks for reading this article.

--

--

Mohaned Zekry

Sr. Software Engineer with +5 years experience in software development lifecycle (SDLC) including analysis, design, development and testing.