MediatR is a library that helps you implement the Mediator pattern, allowing your components to communicate with each other without having direct references.
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.
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.
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."