With the recent news that MediatR will become commercial in its upcoming versions, developers might be considering alternatives. I respect Jimmy Bogard's decision and wish him the best; however, if you are an individual, not a company, and are wondering how to navigate this change, today’s article will be helpful for you.
We will explore how to use this awesome NuGet package, free to use as of the date I am writing this article.
Wolverine is a Next Generation .NET Mediator and Message Bus available as a free nuget package on nuget package manager store.
Let’s start with a fresh .NET 8 Web API :
Name your project:
I am using .NET 8 and choosing the controllers option :
Install the wolverine package :
Register the dependency of this package :
Let’s keep this super simple:
We want to send a “greeting” message and get back a friendly hello. In Wolverine, you don’t implement any special interfaces; everything happens by naming conventions.
First, define a plain request object that carries the user’s name. Then, create a public handler class whose name ends in Handler (or Consumer) and give it a single method that ends in Handle (or Consume). Inside that method, you write your greeting logic.
To send a message from our controller, we use the IMessageBus
interface provided by the package. We inject IMessageBus
into the controller’s constructor and then invoke it like this:
When you call bus.InvokeAsync
With your request, Wolverine scans its known assemblies for any public class named SomethingHandler
(or SomethingConsumer
) and looks for a matching Handle(...)
(or Consume(...)
) method that takes your request type. It then invokes that method and returns the result no interfaces, no manual registrations, no extra boilerplate.
If your request returns a response, then we must specify it next to InvokeAsync<ResponseType>.
Let’s try our request :
Ok but this is not enough we might need to add the fluent validation as well, so we can use this nugget come from same library :
You don’t need to install FluentValidation
separately this packages does it under the hood :
Time to add some validation :
Here we add and modify our dependencies by adding FluentValidator as an option :
And here our validator comes into action :
That looks ugly, but we can do that some day later to make it look better.
Wolverine is packed with powerful features, durable outbox, scheduled delivery, sagas, and more but if you only need its in-process mediator capabilities, you can trim the extras in your Program.cs
. By registering just the core mediator services, you avoid pulling in unnecessary modules and keep your application lean and efficient.
There are 2 ways I can help you:
Enhance your .NET skills by subscribing to my YouTube Channel
Promote yourself to 10,000+ subscribers by sponsoring this newsletter