Skip to content

*Clean Architecture .NET 9, CQRS Template, Production-Ready .NET API, EF Core + Dapper Example, JWT Auth Template, Vertical Slice Architecture*

Notifications You must be signed in to change notification settings

KTajerbashi/CleanArchitecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clean Architecture in .NET

Clean Architecture Diagram

Introduction

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.

Core Principles

Clean Architecture Layers

Key Benefits:

  • 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

Architecture Layers

Layer Structure

1. Domain Layer

  • Contains core business logic and entities
  • Defines interfaces for external interactions
  • Completely independent of other layers

2. Application Layer

  • Implements use cases and application logic
  • Coordinates data flow between layers
  • Depends only on Domain layer

3. Infrastructure Layer

  • Implements persistence, external services
  • Provides concrete implementations of Domain interfaces
  • Depends on Domain and Application layers

4. Presentation Layer (Web API)

  • Handles HTTP requests/responses
  • Contains controllers and DTOs
  • Depends on Application layer

Implemented Features

Core Libraries:

  • ✅ MediatR (CQRS pattern)
  • ✅ Entity Framework Core
  • ✅ Dapper (for optimized queries)
  • ✅ AutoMapper (object mapping)
  • ✅ FluentValidation
  • ✅ Serilog (structured logging)

Security:

  • ✅ JWT Authentication
  • ✅ .NET Identity
  • ✅ BCrypt.NET (password hashing)

API Features:

  • ✅ Swagger/OpenAPI documentation
  • ✅ Health Checks
  • ✅ OpenTelemetry (distributed tracing)
  • ✅ MiniProfiler (performance analysis)

Additional Components:

  • Caching (Cache Manager)
  • Email (MailKit, FluentEmail)
  • File Processing (EPPlus, CsvHelper)
  • Testing (Moq, Bogus)
  • Resilience (Polly)

Implementation Details

Core Libraries

Library Purpose Layer NuGet
MediatR CQRS pattern Application MediatR
AutoMapper Object mapping Application AutoMapper
FluentValidation Validation logic Domain FluentValidation

Data Access

Library Purpose Layer NuGet
EntityFramework Core ORM Infrastructure EFCore
Dapper High-performance queries Infrastructure Dapper
EFCore.SqlServer SQL Server provider Infrastructure EFCore.SqlServer

API Features

Library Purpose Layer NuGet
Swagger API documentation WebApi Swashbuckle
Serilog Structured logging Cross-layer Serilog
HealthChecks System monitoring Infrastructure AspNetCore.HealthChecks

Security

Library Purpose Layer NuGet
JWT Bearer Token authentication WebApi JwtBearer
ASP.NET Identity User management Infrastructure Identity
BCrypt.NET Password hashing Domain BCrypt

Best Practices

C# Best Practices

  1. Keep Architecture Simple

    • Avoid unnecessary complexity
    • Follow SOLID principles
  2. Layer Independence

    • Strict dependency rules (inner layers don't know about outer ones)
    • Depend on abstractions, not concretions
  3. Clean Code

    • Meaningful naming conventions
    • Small, focused classes/methods
    • Consistent coding style
  4. Continuous Refactoring

    • Regular code quality reviews
    • Incremental improvements
    • Technical debt management
  5. Layer Isolation

    • Inner layers never reference outer layers
    • Dependencies flow inward only
  6. Testability

    • 90%+ unit test coverage for Domain/Application
    • Mock all external dependencies
  7. Performance

    • Dapper for read-heavy operations
    • Caching with CacheManager
    • Query optimization with MiniProfiler

Getting Started

  1. Clone the repository
  2. Configure connection strings in appsettings
  3. Run database migrations
  4. Start the WebApi project

Releases

No releases published

Packages

No packages published