Vortex.Mediator
1.1.1
dotnet add package Vortex.Mediator --version 1.1.1
NuGet\Install-Package Vortex.Mediator -Version 1.1.1
<PackageReference Include="Vortex.Mediator" Version="1.1.1" />
<PackageVersion Include="Vortex.Mediator" Version="1.1.1" />
<PackageReference Include="Vortex.Mediator" />
paket add Vortex.Mediator --version 1.1.1
#r "nuget: Vortex.Mediator, 1.1.1"
#:package Vortex.Mediator@1.1.1
#addin nuget:?package=Vortex.Mediator&version=1.1.1
#tool nuget:?package=Vortex.Mediator&version=1.1.1
Vortex.Mediator
Vortex.Mediator is a source-generated mediator library for .NET with support for:
- request/response with
Task<T> - commands with
Task - notifications with multiple handlers
- async streams with
IAsyncEnumerable<T> - pipeline behaviors
- convention-based handlers with compile-time dispatch
The runtime package is Vortex.Mediator. It already brings Vortex.Mediator.Abstractions as a transitive dependency.
Installation
For normal application usage, install only Vortex.Mediator.
dotnet add package Vortex.Mediator
If you need only the public contracts in a shared contracts project, install Vortex.Mediator.Abstractions.
dotnet add package Vortex.Mediator.Abstractions
Registering In DI
using Vortex.Mediator.DependencyInjection;
builder.Services.AddVortexMediator();
You can also scan explicit assemblies:
builder.Services.AddVortexMediator(typeof(Program).Assembly);
Usage Modes
1. Interface-Based Handler
using Vortex.Mediator.Abstractions;
public sealed record CreateUserCommand(string Name) : IRequest<Guid>;
public sealed class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, Guid>
{
public Task<Guid> Handle(CreateUserCommand request, CancellationToken cancellationToken)
{
return Task.FromResult(Guid.NewGuid());
}
}
2. Convention-Based Static Handler
using Vortex.Mediator.Abstractions;
public sealed record CreateUserCommand(string Name) : IRequest<Guid>;
public static class CreateUserEndpoint
{
public static Task<Guid> Handle(
CreateUserCommand command,
IUserRepository repository,
CancellationToken cancellationToken)
{
return repository.CreateAsync(command.Name, cancellationToken);
}
}
3. Convention-Based Instance Handler
using Vortex.Mediator.Abstractions;
public sealed record CreateUserCommand(string Name) : IRequest<Guid>;
public sealed class CreateUserEndpoint(IUserRepository repository)
{
public Task<Guid> Handle(
CreateUserCommand command,
CancellationToken cancellationToken)
{
return repository.CreateAsync(command.Name, cancellationToken);
}
}
For instance handlers, the generated code:
- first tries to resolve the handler from DI
- if it is not registered, falls back to
ActivatorUtilities.CreateInstance
Sending Requests
var userId = await mediator.Send(new CreateUserCommand("Ada"), cancellationToken);
Command without response:
public sealed record RebuildCacheCommand() : IRequest;
await mediator.Send(new RebuildCacheCommand(), cancellationToken);
Publishing Notifications
public sealed record UserCreated(Guid UserId) : INotification;
await mediator.Publish(new UserCreated(userId), cancellationToken);
Interface-based notification handler:
public sealed class SendWelcomeEmailHandler : INotificationHandler<UserCreated>
{
public Task Handle(UserCreated notification, CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
Convention-based notification handler:
public static class UserCreatedEndpoint
{
public static Task Handle(UserCreated notification, IEmailSender emailSender, CancellationToken cancellationToken)
{
return emailSender.SendWelcomeAsync(notification.UserId, cancellationToken);
}
}
Streams
public sealed record GetNumbers(int Count) : IStreamRequest<int>;
public static class NumberStreamEndpoint
{
public static async IAsyncEnumerable<int> Handle(
GetNumbers request,
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
{
for (var index = 1; index <= request.Count; index++)
{
cancellationToken.ThrowIfCancellationRequested();
yield return index;
await Task.Yield();
}
}
}
Consumption:
await foreach (var item in mediator.CreateStream(new GetNumbers(3), cancellationToken))
{
Console.WriteLine(item);
}
Pipeline Behaviors
Response pipeline:
public sealed class LoggingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
{
public async Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken)
{
Console.WriteLine(typeof(TRequest).Name);
return await next();
}
}
Registration:
builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
The same pattern exists for:
IPipelineBehavior<TRequest>for commandsIStreamPipelineBehavior<TRequest, TResponse>for streams
Notes
Vortex.Mediatoruses source generation for dispatch.- Requests, notifications and streams are mapped at compile time.
- Reflection is not used in the runtime dispatch path.
- Convention-based handlers and interface-based handlers can coexist.
Development
- Main runtime package:
src/Vortex.Mediator - Public contracts:
src/Vortex.Mediator.Abstractions - Source generator:
src/Vortex.Mediator.SourceGenerator - Tests:
tests/Vortex.Mediator.Tests
See CONTRIBUTING.md for contribution guidelines.
| 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 is compatible. 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. |
-
net10.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Vortex.Mediator.Abstractions (>= 1.1.1)
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Vortex.Mediator.Abstractions (>= 1.1.1)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
- Vortex.Mediator.Abstractions (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.