Guides & Recipes — Mediator
Practical patterns for writing zero-allocation mediator behaviors and isolated unit tests.
Recipe 1: Implementing a Custom IPipelineBehavior<,>
Section titled “Recipe 1: Implementing a Custom IPipelineBehavior<,>”using System.Threading;using System.Threading.Tasks;using EricksonLopez.Mediator;
namespace Example.MediatorRecipes;
public sealed class TimingBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse> where TRequest : notnull{ public async ValueTask<TResponse> Handle<TNext>( TRequest request, TNext next, CancellationToken cancellationToken) where TNext : struct, INext<TResponse> { return await next.InvokeAsync(); }}Recipe 2: Unit Testing Handlers with FakeMediator
Section titled “Recipe 2: Unit Testing Handlers with FakeMediator”Use EricksonLopez.Mediator.Testing.FakeMediator in presentation/application unit tests to stub responses (SetupCommand, SetupQuery) and assert exact dispatch counts (ShouldHaveReceived<T>(), ReceivedCount<T>()).