femto-backend/Femto.Common/Integration/IEventHandler.cs
2025-05-17 23:47:19 +02:00

24 lines
648 B
C#

namespace Femto.Common.Integration;
public interface IEventHandler
{
Task Handle(IEvent evt, CancellationToken cancellationToken = default);
}
public abstract class EventHandler<T> : IEventHandler
where T : IEvent
{
protected abstract Task Handle(T evt, CancellationToken cancellationToken);
public async Task Handle(IEvent evt, CancellationToken cancellationToken = default)
{
if (evt is not T typedEvt)
{
throw new InvalidOperationException(
$"Event {evt.GetType()} is not of type {typeof(T)}"
);
}
await Handle(typedEvt, cancellationToken);
}
}