Tutorials Clean Architecture & DDD Mastery

MediatR: Implementing the Mediator pattern in .NET

On this page

The In-Process Broker

MediatR is a library that helps you implement the Mediator pattern, allowing your components to communicate with each other without having direct references.

1. One Handler, One Responsibility

Instead of a 10,000-line OrderService with 50 methods, you have 50 small RequestHandler classes. Each handler does exactly one thing (e.g., AddProductToCartHandler). This makes your code incredibly easy to maintain, test, and follow.

2. Decoupling the UI

Your API Controller doesn't inject services. It only injects IMediator. It says: mediator.Send(new CreateOrderCommand(...)). The Controller doesn't know WHO handles the command or HOW. It just knows it sent it. This is the ultimate form of decoupling in Clean Architecture.

3. Architect Insight

Q: "Is MediatR just 'Service Locator' in disguise?"

Architect Answer: "No. Service Locator is used to fetch dependencies. MediatR is used to **dispatch** messages. The key difference is that the 'Call Site' (the Controller) doesn't care about the implementation or the dependencies of the handler. It simply defines an intent. This leads to much cleaner, more 'Message-Driven' code inside your monolith."

Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Clean Architecture & DDD Mastery
Course syllabus
1. Architectural Patterns
2. Domain-Driven Design (DDD) Foundations
3. Advanced DDD Patterns
4. Implementing the Clean Layers
5. Patterns for Data & Logic
6. Enterprise Domain Challenges
7. Testing Clean Architecture
8. Real-World Case Study
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details