.NET Core  

Best-practice architecture for .NET Core projects

βœ… Recommended Architecture: Clean Architecture / Hexagonal / Onion

These modern patterns are domain-driven , decouple infrastructure , and test-friendly .

πŸ”Ή 1. Layers & Responsibilities

  1. Presentation Layer (API/UI)

    • ASP.NET Core Web API / MVC / Razor Pages / Blazor

    • Handles HTTP requests, model binding, validation, and authentication

    • Calls Application layer (never directly the DB or services)

  2. Application Layer (Use Cases)

    • Contains business logic & use cases

    • Uses CQRS (Commands/Queries) with MediatR (best practice in 2025)

    • Defines interfaces for persistence, email, cache, etc.

    • Does not depend on infrastructure implementation

  3. Domain Layer (Core)

    • Contains Entities, Value Objects, Domain Events, Aggregates

    • Pure C# classes, no dependency on EF Core or external libs

    • Business rules live here

  4. Infrastructure Layer

    • Implements interfaces from the Application layer

    • EF Core repositories, third-party services (Braintree, PayPal, AWS, etc.)

    • Logging, Email, File storage, Payment gateways

    • Should not contain business logic

πŸ”Ή 2. Best Practices

  • Dependency Inversion β†’ Infrastructure depends on Application, not the other way around

  • CQRS + MediatR β†’ Separate read (queries) and write (commands) logic

  • FluentValidation for request validation

  • AutoMapper for DTO ↔ Entity mapping

  • Serilog / NLog for logging

  • EF Core Migrations for database versioning

  • Swagger / NSwag for API documentation

  • Unit Testing in Application + Domain

  • Integration Testing in Infrastructure

πŸ”Ή 3. Folder Structure Example

  
    src/
 β”œβ”€β”€ MyProject.Api              -> Presentation (Controllers, Filters, Middlewares)
 β”œβ”€β”€ MyProject.Application      -> Use cases, CQRS, Interfaces, DTOs
 β”œβ”€β”€ MyProject.Domain           -> Entities, Value Objects, Events
 β”œβ”€β”€ MyProject.Infrastructure   -> EF Core, Repositories, Services, Configurations
 └── MyProject.Tests            -> Unit & Integration Tests
  

πŸ”Ή 4. Tech & Tools Suggestions

  • Authentication & Authorization β†’ IdentityServer or JWT

  • Database β†’ EF Core with Repository + Unit of Work pattern

  • Caching β†’ Redis

  • API Versioning β†’ Microsoft.AspNetCore.Mvc.Versioning

  • Observability β†’ OpenTelemetry + Application Insights

  • CI/CD β†’ GitHub Actions / Azure DevOps / Jenkins

  • Containerization β†’ Docker + Kubernetes (future scaling)

πŸ”Ή 5. When to Use This

  • βœ… Medium to large projects

  • βœ… Microservices or modular monoliths

  • βœ… Teams with multiple devs

  • βœ… Long-term maintainability needed

For small projects , a simplified 3-layer architecture (Controller β†’ Service β†’ Repository) is enough.

Arch-image

These three are often confused because they share principles like dependency inversion, separation of concerns, and domain-centric design , but they differ in emphasis and structure.

1️⃣ Clean Architecture

πŸ“Œ Concept

  • Proposed by Robert C. Martin (Uncle Bob).

  • Organizes the system into concentric circles with the domain (entities) at the center.

  • Dependency Rule: All dependencies point inward (towards the domain).

  • Use Cases (Application layer) orchestrate the rules.

βœ… Pros

  • Clear separation of concerns.

  • Technology/framework independent β†’ easy to replace EF Core, UI, or external services.

  • Highly testable.

  • Supports CQRS and MediatR well.

❌ Cons

  • Can feel β€œover-engineered” for small projects.

  • Steep learning curve for new devs.

  • Requires discipline to avoid leaks between layers.

2️⃣ Hexagonal Architecture (Ports & Adapters)

πŸ“Œ Concept

  • Introduced by Alistair Cockburn.

  • Focuses on interactions with the system through ports (interfaces) and adapters (implementations).

  • The application core is isolated, while external systems (DB, APIs, UI, message brokers) connect via adapters.

βœ… Pros

  • Very flexible β†’ easy to swap DBs, UIs, APIs without touching core logic.

  • Strong alignment with microservices.

  • Good for systems with multiple interfaces (REST, gRPC, CLI, etc.).

  • Encourages testability through mocking ports.

❌ Cons

  • Can introduce too many abstractions (interfaces everywhere).

  • Might add boilerplate for simple apps.

  • Needs strong architecture governance to stay clean.

3️⃣ Onion Architecture

πŸ“Œ Concept

  • Proposed by Jeffrey Palermo.

  • Similar to Clean Architecture ,but focuses on layers wrapped around the domain .

  • The domain model is at the core, surrounded by Application Services, Infrastructure, and UI.

  • Dependencies go inward (outer β†’ inner).

βœ… Pros

  • Keeps domain logic independent.

  • Easy to test the domain in isolation.

  • Strong separation of concerns.

  • Good for enterprise applications with rich business rules.

❌ Cons

  • Similar to Clean β†’ adds complexity for small projects.

  • Can be misunderstood β†’ developers sometimes still leak EF Core or infrastructure into the domain.

  • Heavier initial setup.

πŸ”„ Quick Comparison Table

FeatureClean ArchitectureHexagonal ArchitectureOnion Architecture
OriginUncle Bob (2012)Alistair Cockburn (2005)Jeffrey Palermo (2008)
Core FocusDomain + Use CasesPorts & AdaptersDomain-Centric Layers
External SystemsInfrastructure outer ringAdapters (DB, UI, API)Infrastructure outer layer
DependenciesInwardCore β†’ Ports β†’ AdaptersInward
Best FitEnterprise apps, CQRS, APIsMicroservices, multiple interfacesEnterprise apps, DDD-heavy systems
ComplexityMedium-HighMedium-HighMedium

🎯 Simplified Way to Think

  • Clean Architecture = Onion with explicit use cases (CQRS-style).

  • Onion Architecture = Domain-centric layering (classic form).

  • Hexagonal Architecture = More about system boundaries (ports/adapters).