dotnet-cqrs/Svrnty.CQRS.Events.Abstractions/EventHandlers/ICommandHandlerWithEvents.cs

71 lines
3.7 KiB
C#

using System.Threading;
using Svrnty.CQRS.Events.Abstractions.EventStore;
using Svrnty.CQRS.Events.Abstractions.Context;
using System.Threading.Tasks;
namespace Svrnty.CQRS.Events.Abstractions.EventHandlers;
/// <summary>
/// Command handler that can emit strongly-typed correlated events.
/// The framework automatically manages correlation IDs and event emission.
/// </summary>
/// <typeparam name="TCommand">The command type to handle.</typeparam>
/// <typeparam name="TResult">The result type returned by the command.</typeparam>
/// <typeparam name="TEvents">The base type or marker interface for events this command can emit.</typeparam>
public interface ICommandHandlerWithEvents<in TCommand, TResult, TEvents>
where TCommand : class
where TEvents : ICorrelatedEvent
{
/// <summary>
/// Handle the command and emit events using the provided event context.
/// The framework will automatically assign correlation IDs and emit the events.
/// </summary>
/// <param name="command">The command to handle.</param>
/// <param name="eventContext">Context for emitting events with automatic correlation ID management.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The command result.</returns>
Task<TResult> HandleAsync(TCommand command, IEventContext<TEvents> eventContext, CancellationToken cancellationToken = default);
}
/// <summary>
/// Command handler that emits events but returns no result.
/// </summary>
/// <typeparam name="TCommand">The command type to handle.</typeparam>
/// <typeparam name="TEvents">The base type or marker interface for events this command can emit.</typeparam>
public interface ICommandHandlerWithEvents<in TCommand, TEvents>
where TCommand : class
where TEvents : ICorrelatedEvent
{
/// <summary>
/// Handle the command and emit events using the provided event context.
/// The framework will automatically assign correlation IDs and emit the events.
/// </summary>
/// <param name="command">The command to handle.</param>
/// <param name="eventContext">Context for emitting events with automatic correlation ID management.</param>
/// <param name="cancellationToken">Cancellation token.</param>
Task HandleAsync(TCommand command, IEventContext<TEvents> eventContext, CancellationToken cancellationToken = default);
}
/// <summary>
/// Command handler that emits events and returns the result with correlation ID.
/// Use this variant when you need to return the correlation ID to the caller (e.g., for multi-step workflows).
/// </summary>
/// <typeparam name="TCommand">The command type to handle.</typeparam>
/// <typeparam name="TResult">The result type returned by the command.</typeparam>
/// <typeparam name="TEvents">The base type or marker interface for events this command can emit.</typeparam>
public interface ICommandHandlerWithEventsAndCorrelation<in TCommand, TResult, TEvents>
where TCommand : class
where TEvents : ICorrelatedEvent
{
/// <summary>
/// Handle the command and emit events using the provided event context.
/// The framework will automatically assign correlation IDs and emit the events.
/// Returns the result wrapped with the correlation ID for use in follow-up commands.
/// </summary>
/// <param name="command">The command to handle.</param>
/// <param name="eventContext">Context for emitting events with automatic correlation ID management.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>The command result wrapped with the correlation ID.</returns>
Task<TResult> HandleAsync(TCommand command, IEventContext<TEvents> eventContext, CancellationToken cancellationToken = default);
}