DEV Community

Cristiano Coelho
Cristiano Coelho

Posted on

3

Developing your easy alternative to the MediatR library in C#

In recent weeks, there has been a rumor that the famous MediatR library, widely used by the C#/.NET developer community, especially for projects that implement the CQRS pattern, will enter the commercial license.

What I want to make clear, initially, is that one does not need the other to exist. However, when talking about a solution that implements CQRS, developers immediately think of the Mediator pattern and, of course, the library by Jimmy Bogard, who is also behind another famous library in the .NET world, AutoMapper, which, by the way, will also be commercialized.

Of course, you say at first? Oh, what do you mean? Until now it was open source and he had said that he would never make it commercial. (He wrote this when asked once).

OH My God

But that's okay, he has the right to change his mind, especially when circumstances change. He himself explained on his blog that the main reason for charging for use is to maintain the project and the necessary updates, which was previously paid for by the company he worked for.

That said, I wanted to present an easy way to make your own solution to replace MediatR. Of course, here is a much simpler solution, with fewer features, but it serves the basics of handling query and command calls.

Let's get to the solution:

We Have The Solution

First you will have to create 4 interfaces, 2 for commands and 2 for queries. One of them will be the contract for the handler and the other for the dispatcher.

Interfaces

IQueryHandler:

IQueryHandler

IQueryDispatcher:

IQueryDispatcher

ICommandHandler:

ICommandHandler

ICommandDispatcher

ICommandDispatcher

For the magic to happen, we now have to implement the Dispatcher Interfaces to inject the IServiceProvider that will be responsible for retrieving the instances of IQueryHandler and ICommandHandler interfaces. These instances in turn represent the Handlers for the specific queries or commands and their result types.

Dispatchers

QueryDispatcher:

QueryDispatcher

CommandDispatcher:

CommandDispatcher

How Can I use that?

Well, now you can use this implementation and have the separation between queries and commands and the decoupling so desired with the mediator pattern. As an example, I implemented a User query by ID. To do this, you must create 2 classes, one that represents the query and the other the handler. Also create the dto object that will be the return in your API controller.

Dto And Query Handler

UserResponseDto:

UserResponseDto

GetUserByIdQuery:

GetUserByIdQuery

GetUserByIdQueryHandler:

GetUserByIdQueryHandler

Now, you are ready to call this in your controller. In the controller, you need to inject the Dispatcher services and then use them in all the endpoints that are necessary.

To demonstrate the use, I created a controller called UsersController with a GetById method.

Controller

UsersController:

UsersController

Don't forget to add the declarations to inject the services into the dependency container in Program.cs.

Program.cs:

Program.cs

Conclusion:

This is the end of this article. I showed you a very simplified way to have the main functionality of Jimmy Bogard's famous library. A way for you to implement CQRS without depending on an external library. You can create all your queries and commands as you wish, adding them to the necessary use cases. Add anything else you need, such as repository services, logs, etc.

Good work, see you in the next article!

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (1)

Collapse
 
sergi0martin profile image
Sergi0Martin

Good post!
Now that Mediatr is no more free give a try to this alternative OpenMediator 😉

nuget.org/packages/OpenMediator

Jetbrains Survey

Calling all developers!

Participate in the Developer Ecosystem Survey 2025 and get the chance to win a MacBook Pro, an iPhone 16, or other exciting prizes. Contribute to our research on the development landscape.

Take the survey

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️