Design Patterns Mastery

Mediator Pattern: Decoupling components with MediatR

1 Views Updated 5/4/2026

The Mediator Pattern

The Mediator Pattern defines an object that encapsulates how a set of objects interact. It promotes loose coupling by keeping objects from referring to each other explicitly, allowing you to vary their interaction independently.

1. Solving the "Spaghetti" Problem

If 10 objects all need to talk to each other, you have 90 direct connections. This is spaghetti code. With a Mediator, all 10 objects talk ONLY to the Mediator. The Mediator decides who else needs to know.

2. Industry Standard: MediatR

In modern .NET architecture, we use the MediatR library. Instead of a Controller calling a Service, which calls a Repository, the Controller sends a "Request" into the Mediator bus. Some Handler somewhere in the app picks it up.

// Controller
public async Task<IActionResult> Create(CreateUserCommand command) 
{
    // The controller has NO IDEA who handles this!
    await _mediator.Send(command); 
    return Ok();
}

4. Interview Mastery

Q: "What are the pros and cons of using MediatR in an ASP.NET Core project?"

Architect Answer: "The **Pros** are maximum decoupling and easier unit testing. Each hander is a tiny, isolated slice of logic. It also supports 'Pipeline Behaviors' for global logging and validation. The **Cons** are 'Invisible Logic.' Since there is no direct method call, you can't just Ctrl+Click to see where the request goes. It makes debugging more difficult for juniors because the flow of the application becomes abstract."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)