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
<PackageReference Include="LiteMediator.Extensions.DependencyInjection" Version="0.1.2" />
<PackageVersion Include="LiteMediator.Extensions.DependencyInjection" Version="0.1.2" />
<PackageReference Include="LiteMediator.Extensions.DependencyInjection" />
paket add LiteMediator.Extensions.DependencyInjection --version 0.1.2
#r "nuget: LiteMediator.Extensions.DependencyInjection, 0.1.2"
#:package LiteMediator.Extensions.DependencyInjection@0.1.2
#addin nuget:?package=LiteMediator.Extensions.DependencyInjection&version=0.1.2
#tool nuget:?package=LiteMediator.Extensions.DependencyInjection&version=0.1.2
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(...)eIPublisher.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
| 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 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. |
-
net8.0
- LiteMediator.Core (>= 0.1.2)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.