Create clean architecture with ASP.NET Core API

 

Create clean architecture with ASP.NET Core API

Here’s a high-level overview of a clean architecture API setup:

/MyApp

── /API                → Presentation layer (Controllers)

── /Application        → Business logic (use cases, DTOs, interfaces)

── /Domain             → Core models and business rules

── /Infrastructure     → Data access, external services

└── /Persistence        → EF Core repositories and DB context

👉 Step-by-Step Setup :

1.      Create Solution & Project: (use command in terminal for create Solution and project) 👇

dotnet new sln -n MyApp

dotnet new webapi -n MyApp.API

dotnet new classlib -n MyApp.Application

dotnet new classlib -n MyApp.Domain

dotnet new classlib -n MyApp.Infrastructure

dotnet new classlib -n MyApp.Persistence

2.      Reference Project: (use terminal command for add project reference) 👇

 

dotnet sln add **all projects**

dotnet add MyApp.API reference MyApp.Application

dotnet add MyApp.Application reference MyApp.Domain

dotnet add MyApp.Infrastructure reference MyApp.Application

dotnet add MyApp.API reference MyApp.Infrastructure

dotnet add MyApp.Application reference MyApp.Persistence

 

3.     Layer Responsibilities 👇

      • Domain: Entities, enums, value objects
      • Application: Interfaces, use-case logic, DTOs, service contracts
      • Infrastructure: Implementations of services (e.g., email, logging)
      • Persistence: EF Core DbContext, repositories
      • API: Controllers that call Application layer via services

4.     Use Dependency Injection & MediatR 👇

      • Keeps controllers lean and logic testable.
      • Add MediatR to orchestrate requests/commands.

dotnet add package MediatR.Extensions.Microsoft.DependencyInjection

5.      Automapper & FluentValidation (Optional) 👇

    • Use Automapper for DTO-to-entity mapping.
    • Use FluentValidation for clean input validation logic.

Comments

Popular posts from this blog

You can't update multiple tables in one statement

Threading with Mutex