Clean Architecture & DDD Mastery

Unit of Work: Ensuring atomic transactions

1 Views Updated 5/4/2026

Atomic Integrity

The Unit of Work pattern coordinates the work of multiple repositories by creating a single database transaction that covers all of them.

1. All or Nothing

If your business process involves creating a User AND generating an Invoice AND sending an SMS, you want them all to succeed or all to fail. The Unit of Work ensures that you don't end up with a "Ghost User" who has no invoice because the second database call failed.

2. Implementation with EF Core

EF Core's `DbContext` is actually a built-in Unit of Work. When you call await context.SaveChangesAsync(), it wraps all pending changes in a single transaction. In Clean Architecture, we often wrap this in an IUnitOfWork interface to keep our shared persistence logic consistent across the whole application.

3. Architect Insight

Q: "Should the Application Service call SaveChanges?"

Architect Answer: "YES. The Application Service (the Use Case) is the coordinator of the transaction. It should call the repositories to do the work and then call UnitOfWork.CommitAsync() at the very end. This ensures that the 'Use Case' boundary and the 'Database Transaction' boundary are one and the same."

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