Table of Contents

Interface IRequestHandler<TRequest, TResponse>

Namespace
SimpleMediator
Assembly
SimpleMediator.dll

Executes the logic associated with a specific request using Railway Oriented Programming.

public interface IRequestHandler<in TRequest, TResponse> where TRequest : IRequest<TResponse>

Type Parameters

TRequest

Handled request type.

TResponse

Response type returned on completion.

Examples

public sealed class RefundPaymentHandler : IRequestHandler<RefundPayment, Unit>
{
    public async Task<Either<MediatorError, Unit>> Handle(RefundPayment request, CancellationToken cancellationToken)
    {
        var payment = await _paymentGateway.FindAsync(request.PaymentId, cancellationToken);
        if (payment is null)
            return Left(MediatorErrors.NotFound("Payment not found"));

        if (!payment.CanRefund)
            return Left(MediatorErrors.ValidationFailed("Payment cannot be refunded"));

        await _paymentGateway.RefundAsync(request.PaymentId, cancellationToken);
        await _auditTrail.RecordAsync(request.PaymentId, cancellationToken);

        return Right(Unit.Default);
    }
}

Remarks

Handlers should stay lightweight and delegate orchestration to specialized services. The mediator manages their lifetime according to the container configuration.

Handlers return LanguageExt.Either<L, R> to enable explicit error handling without exceptions. Return Right(value) for success or Left(error) for functional failures.

Methods

Handle(TRequest, CancellationToken)

Processes the incoming request and returns either an error or the expected response.

Task<Either<MediatorError, TResponse>> Handle(TRequest request, CancellationToken cancellationToken)

Parameters

request TRequest

Request to handle.

cancellationToken CancellationToken

Cancellation token.

Returns

Task<Either<MediatorError, TResponse>>

Either a MediatorError (Left) representing a functional failure, or the expected response (Right) on success.

Remarks

Use static LanguageExt.Prelude to access Left and Right factory methods. The mediator pipeline will short-circuit on the first Left value encountered.