HamedStack.MiniMediator 1.1.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package HamedStack.MiniMediator --version 1.1.0
                    
NuGet\Install-Package HamedStack.MiniMediator -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HamedStack.MiniMediator" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HamedStack.MiniMediator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="HamedStack.MiniMediator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HamedStack.MiniMediator --version 1.1.0
                    
#r "nuget: HamedStack.MiniMediator, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package HamedStack.MiniMediator@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HamedStack.MiniMediator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=HamedStack.MiniMediator&version=1.1.0
                    
Install as a Cake Tool

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 IServiceCollection extension
  • βœ… 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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