Clean Architecture is a software design approach that promotes maintainability, testability, and flexibility by separating business logic from implementation details. In .NET applications, this architecture helps build robust, scalable systems that can evolve over time without major refactoring.
- Separation of Concerns: Isolates business logic from infrastructure and UI
- Testability: Enables focused unit testing of core logic
- Maintainability: Reduces ripple effects from changes
- Flexibility: Allows swapping implementations without changing core logic
- Contains core business logic and entities
- Defines interfaces for external interactions
- Completely independent of other layers
- Implements use cases and application logic
- Coordinates data flow between layers
- Depends only on Domain layer
- Implements persistence, external services
- Provides concrete implementations of Domain interfaces
- Depends on Domain and Application layers
- Handles HTTP requests/responses
- Contains controllers and DTOs
- Depends on Application layer
- ✅ MediatR (CQRS pattern)
- ✅ Entity Framework Core
- ✅ Dapper (for optimized queries)
- ✅ AutoMapper (object mapping)
- ✅ FluentValidation
- ✅ Serilog (structured logging)
- ✅ JWT Authentication
- ✅ .NET Identity
- ✅ BCrypt.NET (password hashing)
- ✅ Swagger/OpenAPI documentation
- ✅ Health Checks
- ✅ OpenTelemetry (distributed tracing)
- ✅ MiniProfiler (performance analysis)
- Caching (Cache Manager)
- Email (MailKit, FluentEmail)
- File Processing (EPPlus, CsvHelper)
- Testing (Moq, Bogus)
- Resilience (Polly)
Library | Purpose | Layer | NuGet |
---|---|---|---|
MediatR | CQRS pattern | Application | MediatR |
AutoMapper | Object mapping | Application | AutoMapper |
FluentValidation | Validation logic | Domain | FluentValidation |
Library | Purpose | Layer | NuGet |
---|---|---|---|
EntityFramework Core | ORM | Infrastructure | EFCore |
Dapper | High-performance queries | Infrastructure | Dapper |
EFCore.SqlServer | SQL Server provider | Infrastructure | EFCore.SqlServer |
Library | Purpose | Layer | NuGet |
---|---|---|---|
Swagger | API documentation | WebApi | Swashbuckle |
Serilog | Structured logging | Cross-layer | Serilog |
HealthChecks | System monitoring | Infrastructure | AspNetCore.HealthChecks |
Library | Purpose | Layer | NuGet |
---|---|---|---|
JWT Bearer | Token authentication | WebApi | JwtBearer |
ASP.NET Identity | User management | Infrastructure | Identity |
BCrypt.NET | Password hashing | Domain | BCrypt |
-
Keep Architecture Simple
- Avoid unnecessary complexity
- Follow SOLID principles
-
Layer Independence
- Strict dependency rules (inner layers don't know about outer ones)
- Depend on abstractions, not concretions
-
Clean Code
- Meaningful naming conventions
- Small, focused classes/methods
- Consistent coding style
-
Continuous Refactoring
- Regular code quality reviews
- Incremental improvements
- Technical debt management
-
Layer Isolation
- Inner layers never reference outer layers
- Dependencies flow inward only
-
Testability
- 90%+ unit test coverage for Domain/Application
- Mock all external dependencies
-
Performance
- Dapper for read-heavy operations
- Caching with CacheManager
- Query optimization with MiniProfiler
- Clone the repository
- Configure connection strings in appsettings
- Run database migrations
- Start the WebApi project