Clean Architecture & DDD Mastery

Domain Events: Decoupling side effects via events

1 Views Updated 5/4/2026

Eventual Consistency

A Domain Event is something that happened in the domain that you want other parts of the same domain to react to.

1. Decoupling the "And Also"

When a user registers, you might need to send a welcome email, create a default profile, and notify the analytics team. In a messy app, you'd put all this in the RegisterUser method. In DDD, the user entity just raises a UserRegisteredEvent. Other services 'Subscribe' to this event and handle their specific tasks independently.

2. Implementation in .NET

Use an IDomainEvent interface. Entities hold a List<IDomainEvent>. When you save the entity (e.g., in your EF Core SaveChanges override), you publish these events using **MediatR**. This ensures that the primary action and its side effects happen in a clean, decoupled way.

3. Architect Insight

Q: "Should events be synchronous or asynchronous?"

Architect Answer: "Start **Synchronous** using MediatR INotification within the same database transaction. This is easiest to debug. Only move to **Asynchronous** (via SQS/Service Bus) if the side effect is slow (like sending an email) or if you are communicating across different Bounded Contexts. This is the path from Monolith to Microservices."

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