Clean Architecture & DDD Mastery

Validation Strategies: FluentValidation in the App Layer

1 Views Updated 5/4/2026

Defensive Coding

Validation lives in two places in Clean Architecture: **Domain Validation** (Rules of the Entity) and **Application Validation** (Rules of the Use Case).

1. FluentValidation

The industry standard for .NET. It allows you to define complex validation rules in a separate class (e.g., CreateUserCommandValidator). This keeps your command DTOs as clean data-containers and puts the 'Input Validation' logic in its own testable unit.

2. Fail Fast with MediatR

We use a **Validation Pipeline Behavior** in MediatR. Before our handler is even called, the behavior automatically checks all registered validators. If the input is invalid, it throws a ValidationException and stops the request. Your handler can then assume that the data it receives is clean and valid.

3. Architect Insight

Q: "Should I check if a User exists in a Validator?"

Architect Answer: "NO. Data-existence checks (like 'Is this email taken?') require database calls and belong in the **Handler** or a **Domain Service**. Validators should only check 'Format' and 'Structure' (e.g., 'Is the email format correct?', 'Is the age > 18?'). Keep your validators fast and side-effect free."

Clean Architecture & DDD Mastery
1. Architectural Patterns
The Evolution of Architecture: Monolith to Clean Onion Architecture: Dependency Inversion at the core Clean Architecture: The 'Screaming' architecture Hexagonal Architecture (Ports and Adapters)
2. Domain-Driven Design (DDD) Foundations
Ubiquitous Language: Aligning code with business Entities vs Value Objects: Managing identity and state Aggregates & Aggregate Roots: Defining consistency boundaries Bounded Contexts: Handling complexity in large domains
3. Advanced DDD Patterns
Domain Services: When logic doesn't fit in an entity Domain Events: Decoupling side effects via events Repositories: Mediating between domain and data Unit of Work: Ensuring atomic transactions
4. Implementing the Clean Layers
The Domain Layer: Zero dependencies, pure C# The Application Layer: Orchestrating use cases The Infrastructure Layer: Bridging to the outside world The Presentation Layer: Decoupling the UI from logic
5. Patterns for Data & Logic
CQRS (Command Query Responsibility Segregation) MediatR: Implementing the Mediator pattern in .NET Specification Pattern: Encapsulating business rules Policy Pattern: Handling complex authorization rules
6. Enterprise Domain Challenges
Handling Persistence Ignorance with EF Core Mapping Layers: AutoMapper vs Manual Mapping Validation Strategies: FluentValidation in the App Layer Error Handling: Result patterns vs Exceptions
7. Testing Clean Architecture
Unit Testing the Domain: Fast and pure Testing Use Cases with Mocks Integration Testing the Infrastructure ArchUnit .NET: Enforcing architectural rules via tests
8. Real-World Case Study
Refactoring a 'Spaghetti' Monolith to Clean Architecture DDD in Action: Modeling a complex Logistics system