Clean Architecture
Core Principles
- Dependency inversion is the foundation — All dependencies point inward. Domain has zero project references. Application references only Domain. Infrastructure references Application and Domain. Api references all but depends on abstractions. The compiler enforces this via project references.
- Domain owns the rules — Business logic lives in the Domain layer as entity methods, domain services, or specifications. The Domain layer has no knowledge of databases, HTTP, or any framework — only pure C# and .NET primitives.
- Use cases are the unit of work — Each use case (command or query) is a single class in the Application layer. It orchestrates domain objects, persists through abstractions, and returns a result. No "service" classes with 20 methods.
- Infrastructure is a plugin — EF Core, external APIs, email senders, file storage — all live in Infrastructure and implement interfaces defined in Application or Domain. Swap implementations without touching business logic.
- The API layer is thin — Endpoints map HTTP to use cases and use cases to HTTP responses. No business logic in endpoints.
Patterns
Project Layout
DbContext Abstraction (Preferred Over Repository)
Define a minimal interface in Application; implement in Infrastructure:
Why IAppDbContext over IRepository? EF Core's DbSet already IS a repository. Adding another abstraction on top adds indirection without value in most cases.
Use Case Handler (Command)
Use Case Handler (Query)
Domain Entity with Behavior
Thin Endpoint Wiring (IEndpointGroup Auto-Discovery)
Every endpoint group implements IEndpointGroup and is auto-discovered via app.MapEndpoints(). Program.cs never changes when adding new endpoints. See the minimal-api skill for the full IEndpointGroup interface and EndpointExtensions setup.
