Skip to content

Coding-From-Zero-to-Hero/clean-architecture-template

Repository files navigation

Clean Architecture Template

A production-ready .NET 10 template implementing Clean Architecture principles with modern best practices, Docker support, and comprehensive architecture testing.

.NET PostgreSQL Docker License

πŸ—οΈ Architecture Overview

This template follows Clean Architecture principles, organizing code into concentric layers with clear separation of concerns:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Web.Api (Presentation)                β”‚
β”‚                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                β”‚
β”‚                  β”‚   Infrastructure    β”‚                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚              Application (Use Cases)              β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚
β”‚  β”‚  β”‚           Domain (Business Logic)           β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β”‚     SharedKernel (Common Types)       β”‚  β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Layer Dependencies

Web.Api β†’ Infrastructure β†’ Application β†’ Domain β†’ SharedKernel
                         Application β†’ Domain

πŸ“ Project Structure

clean-architecture-template/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ Domain/              # Core business entities and logic
β”‚   β”œβ”€β”€ Application/         # Use cases, interfaces, DTOs
β”‚   β”œβ”€β”€ Infrastructure/      # External implementations (DB, APIs)
β”‚   β”œβ”€β”€ SharedKernel/        # Common types (Result, Error, Entity)
β”‚   └── Web.Api/             # REST API, controllers, middleware
β”œβ”€β”€ tests/
β”‚   └── ArchitectureTests/   # NetArchTest rules enforcement
β”œβ”€β”€ docker-compose.yml       # Full stack orchestration
└── Directory.Packages.props # Central package management

πŸš€ Features

Core Features

  • βœ… Clean Architecture - Strict layer separation with dependency rules
  • βœ… .NET 10 - Latest framework with modern C# features
  • βœ… Docker Ready - Multi-stage builds with docker-compose
  • βœ… PostgreSQL - Configured with EF Core and naming conventions
  • βœ… Serilog - Structured logging with Seq integration
  • βœ… Health Checks - Built-in health monitoring endpoints
  • βœ… Swagger/OpenAPI - Auto-configured API documentation
  • βœ… JWT Authentication - Ready-to-configure authentication

Quality & Testing

  • βœ… Architecture Tests - NetArchTest enforces layer boundaries
  • βœ… SonarAnalyzer - Static code analysis
  • βœ… Central Package Management - Consistent versions
  • βœ… Strict Code Analysis - Treat warnings as errors

Shared Kernel Patterns

  • βœ… Result Pattern - Functional error handling
  • βœ… Domain Events - Event-driven architecture support
  • βœ… Base Entity - Common entity infrastructure

πŸ› οΈ Tech Stack

Layer Technologies
Framework .NET 10, ASP.NET Core
Database PostgreSQL, EF Core 10
Logging Serilog, Seq
Validation FluentValidation
Testing xUnit, NetArchTest, Shouldly
API Docs Swagger/OpenAPI
Container Docker, Docker Compose

πŸš€ Getting Started

Prerequisites

Quick Start

  1. Clone the repository

    git clone https://github.com/yourusername/clean-architecture-template.git
    cd clean-architecture-template
  2. Run with Docker Compose

    docker-compose up --build
  3. Access the application

    • API: http://localhost:5000
    • Swagger UI: http://localhost:5000/swagger
    • Seq Logs: http://localhost:8081
    • PostgreSQL: localhost:5432

Development Setup

# Restore dependencies
dotnet restore

# Run the API (development)
dotnet run --project src/Web.Api

# Run architecture tests
dotnet test tests/ArchitectureTests

πŸ“¦ Solution Structure

Domain Layer

Pure business logic with no external dependencies.

  • Entities
  • Value Objects
  • Domain Events
  • Domain Services

Application Layer

Use cases and application orchestration.

  • Commands & Queries (CQRS ready)
  • DTOs & Mappers
  • Validation (FluentValidation)
  • Service Interfaces

Infrastructure Layer

External concerns implementation.

  • EF Core DbContext & Migrations
  • Repository Implementations
  • External Services
  • JWT Authentication

Web.Api Layer

Presentation layer.

  • Minimal APIs / Endpoints
  • Middleware Pipeline
  • Swagger Configuration
  • Health Checks

πŸ”’ Architecture Enforcement

Architecture tests using NetArchTest ensure layer boundaries are never violated:

// Example rules enforced by tests
Domain β†’ No dependencies on Application/Infrastructure/Presentation
Application β†’ No dependencies on Infrastructure/Presentation
Infrastructure β†’ No dependencies on Presentation

Run tests to verify:

dotnet test --filter "ArchitectureTests"

🐳 Docker Configuration

The template includes a complete Docker setup:

Service Port Description
web-api 5000/5001 ASP.NET Core API
postgres 5432 PostgreSQL database
seq 8081 Log aggregation

βš™οΈ Configuration

Environment Variables

Key configuration in appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Host=postgres;Database=clean-architecture;Username=postgres;Password=postgres"
  },
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.Seq" ],
    "WriteTo": [
      { "Name": "Seq", "Args": { "serverUrl": "http://seq:80" } }
    ]
  }
}

🎯 Usage Example

Result Pattern

public class UserService
{
    public async Task<Result<User>> GetUserByIdAsync(Guid id)
    {
        var user = await _repository.GetByIdAsync(id);
        
        if (user is null)
            return Result.Failure<User>(Error.NotFound);
        
        return Result.Success(user);
    }
}

Domain Event

public class UserCreatedEvent : IDomainEvent
{
    public Guid UserId { get; }
    public DateTime CreatedAt { get; }
}

Built with ❀️ using Clean Architecture principles

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published