Interface INotificationHandler<TNotification>
- Namespace
- SimpleMediator
- Assembly
- SimpleMediator.dll
Processes a notification published by the mediator using Railway Oriented Programming.
public interface INotificationHandler<in TNotification> where TNotification : INotification
Type Parameters
TNotificationNotification type being handled.
Examples
public sealed class AuditReservationHandler : INotificationHandler<ReservationCreatedNotification>
{
public async Task<Either<MediatorError, Unit>> Handle(ReservationCreatedNotification notification, CancellationToken cancellationToken)
{
var reservation = await _repository.FindAsync(notification.ReservationId, cancellationToken);
if (reservation is null)
return Left(MediatorErrors.NotFound("Reservation not found for audit"));
await _auditLog.RecordAsync(notification.ReservationId, cancellationToken);
return Right(Unit.Default);
}
}
Remarks
Handlers run sequentially following the container resolution order. They must be idempotent and tolerate the presence of multiple consumers.
Handlers return LanguageExt.Either<L, R> to enable explicit error handling without exceptions.
Return Right(Unit.Default) for success or Left(error) if the handler cannot process the notification.
The first handler that returns Left will stop the notification propagation (fail-fast).
Methods
Handle(TNotification, CancellationToken)
Executes the logic associated with the received notification.
Task<Either<MediatorError, Unit>> Handle(TNotification notification, CancellationToken cancellationToken)
Parameters
notificationTNotificationEvent or signal to process.
cancellationTokenCancellationTokenToken to cancel the operation when needed.
Returns
- Task<Either<MediatorError, Unit>>
Either a MediatorError (Left) if the handler cannot process the notification, or LanguageExt.Unit (Right) on successful processing.
Remarks
Use static LanguageExt.Prelude to access Left and Right factory methods.
If this handler returns Left, subsequent handlers will not be executed (fail-fast behavior).