Skip to content

Search EricksonLopez.* Ecosystem

Type a library name, NuGet package ID, .NET type, or architectural concept to search across the portal.

Getting Started with EricksonLopez.Mediator

Install EricksonLopez.Mediator and EricksonLopez.Mediator.Generator (2.0.0 on NuGet) so Roslyn emits AddEricksonLopezMediator() at compile time.

dotnet add package EricksonLopez.Mediator --version 2.0.0
Terminal window
dotnet add package EricksonLopez.Mediator.Generator --version 2.0.0
dotnet add package EricksonLopez.Mediator.Result --version 2.0.0

AddEricksonLopezMediator() is generated into namespace Microsoft.Extensions.DependencyInjection;, and handlers return ValueTask<TResponse>:

using System;
using System.Threading;
using System.Threading.Tasks;
using EricksonLopez.Mediator;
using EricksonLopez.Result;
using Microsoft.Extensions.DependencyInjection;
namespace Example.MediatorQuickStart;
public sealed record CreateOrderCommand(Guid CustomerId, decimal Amount) : ICommand<Result<Guid>>;
public sealed class CreateOrderHandler : ICommandHandler<CreateOrderCommand, Result<Guid>>
{
public ValueTask<Result<Guid>> Handle(CreateOrderCommand command, CancellationToken cancellationToken)
{
if (command.Amount <= 0m)
{
return ValueTask.FromResult(
Result<Guid>.Failure(Error.Validation("Order.InvalidAmount", "Amount must exceed zero.")));
}
return ValueTask.FromResult(Result<Guid>.Success(Guid.CreateVersion7()));
}
}
public static class MediatorUsage
{
public static void Register(IServiceCollection services)
{
services.AddEricksonLopezMediator();
}
public static async ValueTask<Result<Guid>> DispatchAsync(ISender sender, CancellationToken ct)
{
var command = new CreateOrderCommand(Guid.CreateVersion7(), 125.00m);
return await sender.SendCommand<CreateOrderCommand, Result<Guid>>(command, ct);
}
}