TaskHub.Shared

Events Abstractions

The TaskHub.Shared.Events.Abstractions module defines the core interfaces and base classes for the event-driven architecture within the system.

IEvent & EventBase

Every event in the system must implement IEvent.

public interface IEvent
{
    DateTime CreatedAt { get; }
}

EventBase provides a default implementation that sets CreatedAt to DateTime.UtcNow.


ICrossEvent & CrossEventBase

Cross-events (or Integration Events) are used for communication between different microservices or modules. They often carry more metadata and are designed for serialization.

public interface ICrossEvent : IEvent
{
    Guid EventId { get; }
    string EventName { get; }
}

IEventsBus

The IEventsBus is responsible for dispatching cross-events.

public interface IEventsBus
{
    Task SendAsync<TEvent>(TEvent request, CancellationToken ct)
        where TEvent : ICrossEvent;
}

Unlike the ICommandsBus, the IEventsBus is typically asynchronous and fire-and-forget from the perspective of the publisher’s business logic (often implemented via an Outbox pattern).


IEventHandler

Handlers for events must implement this interface.

public interface IEventHandler<in TRequest> where TRequest : IEvent
{
    Task HandleAsync(TRequest request, CancellationToken ct);
}

Key Differences from Command Handlers:

  1. Multiple Handlers: A single event can have multiple handlers.
  2. No Result: Event handlers do not return a value.
  3. Reliability: Event handlers are often executed in a separate background process or worker.