Clean Architecture & DDD Mastery

Repositories: Mediating between domain and data

1 Views Updated 5/4/2026

The Persistence Gateway

A Repository is an abstraction that makes your domain think it's dealing with an in-memory collection of objects, hiding the complexity of SQL, Mongo, or APIs.

1. The Interface-First Rule

In Clean Architecture, the **Interface** for the repository lives in the **Domain/Core** layer. The **Implementation** lives in the **Infrastructure** layer. This means your business rules never depend on DbContext or SQL strings. They only depend on IOrderRepository.

2. Aggregate Focus

You should only have one repository per **Aggregate Root**. You don't need an OrderItemRepository if an Order is your Aggregate Root. You load the whole Order, modify its items, and save the Order. The repository handles the graph of objects as a single unit.

3. Architect Insight

Q: "Is the Repository pattern dead because of EF Core?"

Architect Answer: "Technically, DbSet is a repository. But for **Clean Architecture**, the answer is NO, it's not dead. A custom repository allows you to hide 'leakage' of persistence logic (like .Include() or complex SQL optimizations) from your domain. It also makes Unit Testing your domain 10x easier because you can mock a simple interface instead of trying to mock the massive EF DbContext."

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