All Projects
Backendintermediate View on GitHub

CleanArch Starter

Production-ready Clean Architecture template with CQRS, MediatR, and EF Core

1–2 hours to set up12 technologies5 guided steps

About This Project

A ready-to-clone .NET 8 solution built on Clean Architecture with CQRS and MediatR. Covers Domain, Application, Infrastructure, and API layers — with FluentValidation pipeline behaviours, global error handling via ProblemDetails, Serilog structured logging, EF Core with PostgreSQL, and a full test suite using xUnit, Moq, and Testcontainers.

What You'll Learn

Understand the four layers of Clean Architecture and their responsibilities
Implement CQRS with MediatR Commands, Queries, and pipeline behaviors
Wire FluentValidation as a cross-cutting concern via MediatR pipeline
Handle errors globally with ProblemDetails instead of try/catch in every controller
Write testable code using the Repository pattern and dependency injection

Key Features

4-layer Clean Architecture: Domain, Application, Infrastructure, API
CQRS with MediatR — Commands, Queries, and Notifications separated
FluentValidation as a MediatR pipeline behavior (no controller validation code)
Global error handling with ProblemDetails (RFC 9457)
Serilog structured logging with request/response enrichment
EF Core with Repository and Unit of Work patterns
Domain events via MediatR notifications
xUnit + Moq for unit tests; Testcontainers for integration tests
Docker + docker-compose for local development
Scalar API documentation out of the box

Project Structure

directory tree
CleanArch-Starter/
├── src/
│   ├── CleanArch.Domain/          # Entities, value objects, domain events
│   │   ├── Entities/
│   │   ├── Events/
│   │   └── Interfaces/            # Repository interfaces
│   ├── CleanArch.Application/     # CQRS handlers, validators, DTOs
│   │   ├── Commands/
│   │   ├── Queries/
│   │   ├── Validators/
│   │   └── Behaviours/            # MediatR pipeline (logging, validation)
│   ├── CleanArch.Infrastructure/  # EF Core, repositories, migrations
│   └── CleanArch.Api/             # Controllers, middleware, DI config
├── tests/
│   ├── CleanArch.Unit/            # Handler tests with Moq
│   └── CleanArch.Integration/     # Testcontainers + WebApplicationFactory
└── docker-compose.yml

Setup Guide

1

Clone the repository

Clone CleanArch Starter and navigate into the solution.

bash
git clone https://github.com/asmanasir/CleanArch-Starter.git
cd CleanArch-Starter
2

Start PostgreSQL with Docker

Spin up the database — the app uses PostgreSQL via EF Core.

bash
docker-compose up -d db
3

Run EF Core migrations

Create the database schema from the existing migrations.

bash
dotnet ef database update --project src/CleanArch.Infrastructure

Running the Project

1

Run the API

Start the API — Scalar docs open at /scalar/v1.

bash
dotnet run --project src/CleanArch.Api
2

Run the test suite

Execute unit tests (Moq) and integration tests (Testcontainers). Testcontainers spins up a real PostgreSQL instance for integration tests.

bash
dotnet test

Project Info

CategoryBackend
Difficultyintermediate
Setup time1–2 hours to set up
Technologies12 tools

Tech Stack

ASP.NET Core (.NET 8)Clean ArchitectureCQRSMediatREF Core 8PostgreSQLFluentValidationSerilogxUnitMoqTestcontainersDocker

Prerequisites

  • .NET 8 SDK installed
  • Docker Desktop installed
  • Basic C# knowledge (classes, interfaces, async/await)
View Source on GitHub
L

Learnixo

Project Author

The starting point most teams wish they had from day one. Clone it, rename the namespace, and you have a production-ready architecture — not a tutorial that falls apart the moment you add a second feature.