LiteMediator.Extensions.DependencyInjection 0.1.2

dotnet add package LiteMediator.Extensions.DependencyInjection --version 0.1.2
                    
NuGet\Install-Package LiteMediator.Extensions.DependencyInjection -Version 0.1.2
                    
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="LiteMediator.Extensions.DependencyInjection" Version="0.1.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LiteMediator.Extensions.DependencyInjection" Version="0.1.2" />
                    
Directory.Packages.props
<PackageReference Include="LiteMediator.Extensions.DependencyInjection" />
                    
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 LiteMediator.Extensions.DependencyInjection --version 0.1.2
                    
#r "nuget: LiteMediator.Extensions.DependencyInjection, 0.1.2"
                    
#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 LiteMediator.Extensions.DependencyInjection@0.1.2
                    
#: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=LiteMediator.Extensions.DependencyInjection&version=0.1.2
                    
Install as a Cake Addin
#tool nuget:?package=LiteMediator.Extensions.DependencyInjection&version=0.1.2
                    
Install as a Cake Tool

LiteMediator

Implementação basica do padrão mediator/CQRS para .NET — pensada como alternativa livre . Cobre o essencial usado no dia a dia com MediatR:

  • IRequest<TResponse> / IRequest + IRequestHandler<TRequest, TResponse>
  • INotification + INotificationHandler<TNotification> (publish/subscribe com múltiplos handlers)
  • ISender.Send(...) e IPublisher.Publish(...)
  • IPipelineBehavior<TRequest, TResponse> (middlewares/decorators em torno do handler)

O dispatch é feito via reflection cacheada por tipo de request/notification (mesma técnica usada historicamente pelo MediatR) — a reflection só acontece na primeira chamada de cada tipo; as seguintes usam apenas o cache.

Instalação

dotnet add package LiteMediator.Core
dotnet add package LiteMediator.Extensions.DependencyInjection

Uso

// Program.cs
services.AddLiteMediator(cfg =>
{
    cfg.RegisterServicesFromAssemblyContaining<Program>();
    cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
});
public sealed record Ping(string Message) : IRequest<string>;

public sealed class PingHandler : IRequestHandler<Ping, string>
{
    public Task<string> Handle(Ping request, CancellationToken cancellationToken) =>
        Task.FromResult($"Pong: {request.Message}");
}

// em qualquer lugar com ISender injetado:
var response = await sender.Send(new Ping("oi"));

Request sem retorno:

public sealed record DeleteThing(int Id) : IRequest;

public sealed class DeleteThingHandler : RequestHandlerBase<DeleteThing>
{
    protected override Task Handle(DeleteThing request, CancellationToken cancellationToken)
    {
        // ...
        return Task.CompletedTask;
    }
}

Notification com múltiplos handlers:

public sealed record ThingCreated(string Name) : INotification;

public sealed class AuditLogHandler : INotificationHandler<ThingCreated> { /* ... */ }
public sealed class EmailHandler : INotificationHandler<ThingCreated> { /* ... */ }

await publisher.Publish(new ThingCreated("widget")); // chama os dois handlers

Pipeline behavior:

public sealed class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
    where TRequest : IRequest<TResponse>
{
    public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken ct)
    {
        Console.WriteLine($"Handling {typeof(TRequest).Name}");
        var response = await next(ct);
        Console.WriteLine($"Handled {typeof(TRequest).Name}");
        return response;
    }
}

Migrando do MediatR

A API foi desenhada para ficar próxima da do MediatR — na maioria dos projetos, migrar é trocar o using MediatR; por using LiteMediator; e services.AddMediatR(...) por services.AddLiteMediator(...). Principais diferenças:

MediatR LiteMediator
IMediator ISender / IPublisher (separados)
services.AddMediatR(cfg => ...) services.AddLiteMediator(cfg => ...)
cfg.RegisterServicesFromAssembly(...) idêntico
cfg.AddOpenBehavior(...) idêntico

Benchmarks

Projeto benchmarks/LiteMediator.Benchmarks compara LiteMediator, MediatR e chamada direta (BenchmarkDotNet). Rode com:

cd benchmarks/LiteMediator.Benchmarks
dotnet run -c Release

Licença

MIT

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 was computed.  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
0.1.2 102 8/28/2026
0.1.1 89 8/28/2026
0.1.0 93 8/28/2026