.NET Clean Architecture

Domain centric architectures, like clean architecture, have inner architectural cores that model the domain. Dependency inversion is king, with inner layers defining abstractions and interfaces and outer layers implementing them. Clean architecture is a good fit when aligning to Domain Driven Design (DDD), dealing with complex business logic, high testability is desirable and/or working in a large team, as the architecture can enforce design policies. Guiding Principles Clean Architecture Layers Domain layer Entities Value Objects Domain Events Domain Services Interfaces Results and Exceptions Application layer: The Use Case Orchestrator Application Layer Key Responsibilities Use Case Orchestration Higher Order Business Logic Cross Cutting Concerns Exception Translation & Handling Dependency Injection Hub What the Application Layer Does NOT Do Example Application Service Dependency Injection and MediatR Bootstrapping CQRS Abstractions Handling Domain Events Cross Cutting Concerns with MediatR Pipelines Logging Pipeline Validation Pipeline with FluentValidation Infrastructure layer Infrastructure Layer Key Responsibilities Data Persistence and Access External Service Integration Cross Cutting Concerns Implementation Event Handling Infrastructure What the Infrastructure Layer Does NOT Do Example Concrete Provider for IDateTimeProvider EF Core Setup Integrating Domain Entities with EF Core Publishing Domain Events in the Unit of Work Handling Race Conditions with Optimistic Concurrency Distributed Cache Service Presentation layer Presentation Layer Key Responsibilities What the Presentation Layer Does NOT Do API Controllers and Endpoints Seed Data and EF Migrations Authentication (authn) with Keycloak Authorization (authz) Role-based Authorization Permission-based (Policy) Authorization Resource-based Authorization Structured Logging Health Checks .NET Implementation Tips General .NET Tips Domain Layer .NET Tips Application Layer .NET Tips Infrastructure Layer .NET Tips Presentation Layer .NET Tips Structured Logging Serilog Serilog and Seq Setup Guide Outbox Pattern The Problem The Solution Key Benefits Outbox .NET Implementation Outbox Message Definition Transactionally Publish Domain Events as Outbox Messages Background Worker Job with Quartz.NET Hookup Dependency Injection ASP.NET Core Minimal APIs Controller to Minimal API Conversion Cookbook Centralising Route Opinions with Route Groups Testing Domain Layer Unit Testing Application Layer Unit Testing Mocking with NSubstitute Application Layer Integration Testing with TestContainers Troubleshooting Accessing Internal Symbols Bonus: Contemporary .NET gems Primary Constructors Switch Expressions Records Async Tips MediatR IRequest and IRequestHandler - Request/Response Publishing INotification and INotificationHandler - Pub/Sub Publishing MediatR.Contracts Package Visual Studio and Roslyn Code Quality Level Ups dotnet CLI Tips Guiding Principles High level qualities that a good software architecture should (and enforce) strive for; maintainability, testability and loose coupling. ...

May 29, 2025 · 57 min