some changes
This commit is contained in:
parent
4ec9720541
commit
b47bac67ca
37 changed files with 397 additions and 190 deletions
6
Femto.Common/Integration/Event.cs
Normal file
6
Femto.Common/Integration/Event.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace Femto.Common.Integration;
|
||||
|
||||
public abstract record Event : IEvent
|
||||
{
|
||||
public Guid EventId { get; } = Guid.CreateVersion7();
|
||||
}
|
|
@ -2,7 +2,7 @@ using MediatR;
|
|||
|
||||
namespace Femto.Common.Integration;
|
||||
|
||||
public interface IIntegrationEvent : INotification
|
||||
public interface IEvent : INotification
|
||||
{
|
||||
public Guid EventId { get; }
|
||||
}
|
13
Femto.Common/Integration/IEventBus.cs
Normal file
13
Femto.Common/Integration/IEventBus.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
namespace Femto.Common.Integration;
|
||||
|
||||
public interface IEventBus : IEventPublisher
|
||||
{
|
||||
public delegate Task Subscriber(IEvent evt, CancellationToken cancellationToken);
|
||||
void Subscribe(Subscriber subscriber);
|
||||
|
||||
}
|
||||
|
||||
public interface IEventPublisher
|
||||
{
|
||||
Task Publish<T>(T evt) where T : IEvent;
|
||||
}
|
24
Femto.Common/Integration/IEventHandler.cs
Normal file
24
Femto.Common/Integration/IEventHandler.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
namespace Femto.Common.Integration;
|
||||
|
||||
public interface IIntegrationEventBus
|
||||
{
|
||||
void Subscribe<T>() where T : IIntegrationEvent;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue