using System; using System.Threading; using System.Threading.Tasks; namespace Svrnty.CQRS.Events.Abstractions.Sagas; /// /// Represents a long-running business process (saga) that coordinates multiple steps. /// /// /// /// Sagas implement distributed transactions using compensation rather than two-phase commit. /// Each step has a corresponding compensation action that undoes its effects. /// /// /// Saga Pattern: /// - Execute steps sequentially /// - If a step fails, execute compensations in reverse order /// - Supports timeouts, retries, and state persistence /// /// public interface ISaga { /// /// Unique identifier for this saga instance. /// string SagaId { get; } /// /// Correlation ID linking this saga to related events/commands. /// string CorrelationId { get; } /// /// Name of the saga type (for tracking and monitoring). /// string SagaName { get; } } /// /// Represents a single step in a saga. /// public interface ISagaStep { /// /// Name of this step. /// string StepName { get; } /// /// Execute the step's action. /// /// The saga execution context. /// Cancellation token. /// Task representing the async operation. Task ExecuteAsync(ISagaContext context, CancellationToken cancellationToken = default); /// /// Compensate (undo) the step's action. /// /// The saga execution context. /// Cancellation token. /// Task representing the async operation. Task CompensateAsync(ISagaContext context, CancellationToken cancellationToken = default); } /// /// Context available to saga steps during execution. /// public interface ISagaContext { /// /// The saga instance. /// ISaga Saga { get; } /// /// Current state of the saga. /// SagaState State { get; } /// /// Saga data (shared state across steps). /// ISagaData Data { get; } /// /// Get a value from saga data. /// T? Get(string key); /// /// Set a value in saga data. /// void Set(string key, T value); /// /// Check if a key exists in saga data. /// bool Contains(string key); } /// /// Saga data storage (key-value pairs). /// public interface ISagaData { /// /// Get a value. /// T? Get(string key); /// /// Set a value. /// void Set(string key, T value); /// /// Check if a key exists. /// bool Contains(string key); /// /// Get all data as dictionary. /// System.Collections.Generic.IDictionary GetAll(); } /// /// State of a saga instance. /// public enum SagaState { /// /// Saga has not started yet. /// NotStarted = 0, /// /// Saga is currently executing steps. /// Running = 1, /// /// Saga completed successfully. /// Completed = 2, /// /// Saga is compensating (rolling back). /// Compensating = 3, /// /// Saga was compensated (rolled back). /// Compensated = 4, /// /// Saga failed and could not be compensated. /// Failed = 5, /// /// Saga is paused waiting for external event. /// Paused = 6 }