HamedStack.MiniMediator
1.1.0
dotnet add package HamedStack.MiniMediator --version 1.1.0
NuGet\Install-Package HamedStack.MiniMediator -Version 1.1.0
<PackageReference Include="HamedStack.MiniMediator" Version="1.1.0" />
<PackageVersion Include="HamedStack.MiniMediator" Version="1.1.0" />
<PackageReference Include="HamedStack.MiniMediator" />
paket add HamedStack.MiniMediator --version 1.1.0
#r "nuget: HamedStack.MiniMediator, 1.1.0"
#:package HamedStack.MiniMediator@1.1.0
#addin nuget:?package=HamedStack.MiniMediator&version=1.1.0
#tool nuget:?package=HamedStack.MiniMediator&version=1.1.0
HamedStack.MiniMediator
HamedStack.MiniMediator is a lightweight, zero-dependency, performance-friendly mediator library built for .NET. It provides an abstraction for sending commands/queries and publishing events in a clean, decoupled way β inspired by MediatR but simplified for minimal overhead, making it easy to replace MediatR if you're looking for something more lightweight and flexible.
π¦ Features
- β Request/Response (Command-Query) pattern
- β Notification pattern (Pub/Sub to multiple handlers)
- β Pipeline behaviors (Middleware support)
- β
DI-ready with
IServiceCollectionextension - β Runtime dynamic dispatch support
- β No external package dependencies
- β Similar API to MediatR, making it easy to replace
π Quick Start
1. Define a Request + Handler
public class Ping : IRequest<string>
{
public string Message { get; set; }
}
public class PingHandler : IRequestHandler<Ping, string>
{
public Task<string> Handle(Ping request, CancellationToken cancellationToken)
{
return Task.FromResult($"Pong: {request.Message}");
}
}
2. Define a Notification + Handler
public class UserCreated : INotification
{
public string UserName { get; set; }
}
public class SendWelcomeEmail : INotificationHandler<UserCreated>
{
public Task Handle(UserCreated notification, CancellationToken cancellationToken)
{
Console.WriteLine($"Welcome email sent to {notification.UserName}");
return Task.CompletedTask;
}
}
π Registering in Dependency Injection
You can register the MiniMediator services into your DI container by calling AddMiniMediator.
Registering with specific assemblies:
builder.Services.AddMiniMediator(typeof(Program).Assembly);
This will scan the given assembly for any request handlers and notification handlers.
Registering with all assemblies in the current AppDomain:
If you donβt provide any assemblies, MiniMediator will automatically scan all non-system assemblies in the current AppDomain for handler implementations.
builder.Services.AddMiniMediator();
This is useful when you want to automatically discover and register all handlers in the app, eliminating the need to manually specify each assembly.
βοΈ Using IMediator
public class SomeService
{
private readonly IMediator _mediator;
public SomeService(IMediator mediator)
{
_mediator = mediator;
}
public async Task DoSomething()
{
// Sending request and getting response
var result = await _mediator.Send(new Ping { Message = "Hello" });
Console.WriteLine(result); // Output: Pong: Hello
// Publishing a notification
await _mediator.Publish(new UserCreated { UserName = "Hamed" });
}
}
π§± Pipeline Behavior Example
Create a behavior to log request processing times:
public class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull
{
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var name = typeof(TRequest).Name;
Console.WriteLine($"[START] Handling {name}");
var response = await next();
Console.WriteLine($"[END] Finished {name}");
return response;
}
}
π§ Registering Pipeline Behaviors
Add them to your DI container:
builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
Pipeline behaviors can be stacked, and they execute in the order they are registered (outermost to innermost).
π‘ Advanced Use: Dynamic Dispatch
You can send dynamically typed requests or notifications:
object dynamicRequest = new Ping { Message = "Dynamic Hello" };
var result = await _mediator.Send(dynamicRequest); // returns object
object dynamicNotification = new UserCreated { UserName = "DynamicUser" };
await _mediator.Publish(dynamicNotification);
π Interfaces Overview
| Interface | Description |
|---|---|
IRequest |
Marker for requests with no return value |
IRequest<TResponse> |
Marker for requests with a return value |
INotification |
Marker for notifications (pub-sub) |
IRequestHandler<TRequest> |
Handler for void requests |
IRequestHandler<TRequest, TResponse> |
Handler for response-returning requests |
INotificationHandler<TNotification> |
Handler for notifications |
IPipelineBehavior<TRequest, TResponse> |
Middleware behavior in the pipeline |
IMediator |
Mediator interface to send/publish |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
-
net9.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|