Use Case: Migrating from MediatR to SlimMessageBus
The SlimMessageBus in-memory provider can replace the need to use the MediatR library:
- It has similar semantics and has the interceptor pipeline enabling the addition of custom behavior.
- The generic interceptors can introduce common behavior like logging, authorization, or audit of messages.
- The FluentValidation plugin can introduce request/command/query validation.
- The external communication can be layered on top of SlimMessageBus, enabling one library for in-memory and out-of-process messaging (Hybrid Provider).
See the CQRS and FluentValidation samples.
Example: Migrating from MediatR to SlimMessageBus
Below are examples demonstrating the migration steps.
Using MediatR in Program.cs
Here is a typical setup for MediatR:
using MediatR;
var builder = WebApplication.CreateBuilder(args);
// Register MediatR services
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<Program>();
});
var app = builder.Build();
app.MapGet("/ping", async (IMediator mediator) =>
{
return await mediator.Send(new PingRequest());
});
app.Run();
// Define a simple request and handler
public record PingRequest : IRequest<string>;
public class PingRequestHandler : IRequestHandler<PingRequest, string>
{
public Task<string> Handle(PingRequest request, CancellationToken cancellationToken)
{
return Task.FromResult("Pong");
}
}
Migrating to SlimMessageBus in Program.cs
Install required NuGet packages:
dotnet add package SlimMessageBus.Host.Memory
Example code setup:
using SlimMessageBus;
using SlimMessageBus.Host.Memory;
var builder = WebApplication.CreateBuilder(args);
// Configure SlimMessageBus with In-Memory provider
builder.Services.AddSlimMessageBus(mbb =>
{
mbb.WithProviderMemory().AutoDeclareFrom(typeof(Program).Assembly);
});
var app = builder.Build();
app.MapGet("/ping", async (IMessageBus bus) =>
{
return await bus.Send(new PingRequest());
});
app.Run();
// Define request and handler compatible with SlimMessageBus
public record PingRequest : IRequestMessage<string>;
public class PingRequestHandler : IRequestHandler<PingRequest, string>
{
public async Task<string> OnHandle(PingRequest request, CancellationToken cancellationToken)
{
return "Pong";
}
}
Key Changes:
- Replace
IMediator
withIMessageBus
. - Change request interface from
IRequest
toIRequestMessage
. - Register SlimMessageBus using
AddSlimMessageBus
and configure withWithProviderMemory
. - Handlers now implement
IRequestHandler<TRequest, TResponse>
with methodOnHandle()
instead of MediatR’sHandle()
.
Additional Capabilities in SlimMessageBus:
- Interceptors: You can easily add cross-cutting concerns such as logging, authorization, or auditing.
- FluentValidation Integration: Direct support for request validation via FluentValidation.
- Hybrid Messaging: Seamlessly transition from in-memory to out-of-process messaging if your application evolves (or combine both).
For more advanced usage and additional features, please review the SlimMessageBus documentation.
- Hybrid Messaging: Seamlessly transition from in-memory to out-of-process messaging if your application evolves (or combine both).
For more advanced usage and additional features, please review the SlimMessageBus documentation.