Clean Architecture & DDD Mastery

CQRS (Command Query Responsibility Segregation)

1 Views Updated 5/4/2026

Separation of Concerns

CQRS is a pattern that tells us to use different models for reading data and writing data.

1. Commands vs Queries

**Commands:** Actions that change the state of the system (Create, Update, Delete). They return nothing (or just an ID/Status). They focus on business rules and consistency.
**Queries:** Actions that read data. They return a value. They focus on performance and satisfying the UI's specific data needs.

2. Why separate them?

In a standard app, we use a single 'Repository' that returns Entities. But the UI often needs a 'View Model' that combines 5 different entities. By using CQRS, we can optimize our Read side (using Dapper or raw SQL for speed) without affecting our complex Write side (using EF Core and DDD for consistency).

3. Architect Insight

Q: "Does CQRS require two databases?"

Architect Answer: "NO. That is 'Physical CQRS'. You can start with 'Logical CQRS', where you simply have different classes for Commands and Queries but they both talk to the same SQL database. This still gives you 80% of the benefits (simplicity and clean code) without the 100% of the headache of keeping two databases in sync."

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