β
Recommended Architecture: Clean Architecture / Hexagonal / Onion
These modern patterns are domain-driven , decouple infrastructure , and test-friendly .
πΉ 1. Layers & Responsibilities
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)
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
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
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
π Quick Comparison Table
Feature | Clean Architecture | Hexagonal Architecture | Onion Architecture |
---|
Origin | Uncle Bob (2012) | Alistair Cockburn (2005) | Jeffrey Palermo (2008) |
Core Focus | Domain + Use Cases | Ports & Adapters | Domain-Centric Layers |
External Systems | Infrastructure outer ring | Adapters (DB, UI, API) | Infrastructure outer layer |
Dependencies | Inward | Core β Ports β Adapters | Inward |
Best Fit | Enterprise apps, CQRS, APIs | Microservices, multiple interfaces | Enterprise apps, DDD-heavy systems |
Complexity | Medium-High | Medium-High | Medium |
π― 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).