Clean Architecture & DDD Mastery

Integration Testing the Infrastructure

1 Views Updated 5/4/2026

The Real World

Integration tests verify that your code actually works with real external systems like SQL Server, Redis, or an external API.

1. Testcontainers

The modern standard for integration testing. Instead of managing a 'shared' dev database, Testcontainers spins up a real SQL Server inside a Docker container just for your test run. It's clean, isolated, and ensures that your EF Core configurations (like unique constraints) are actually working in the real engine.

2. Database Reset

Use a library like **Respawn** to quickly reset the state of your test database between every test. This avoids 'pollution' where Test A fails because Test B left data in the table. Integration tests are slower than unit tests, but they provide the 'Sleep-at-night' assurance that the plumbing is correct.

3. Architect Insight

Q: "Should I write integration tests for every repository method?"

Architect Answer: "Focus on the **Critical Paths**. Test complex queries, unique index violations, and multi-table transactions. Don't waste time testing simple 'Get By ID' calls unless there's custom logic involved. Your integration test suite should be the 'Heavy Artillery' that you roll out for the difficult parts of your infrastructure."

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