using System; using System.Threading; using System.Threading.Tasks; namespace Svrnty.CQRS.Sagas.Abstractions; /// /// Fluent builder for defining saga steps. /// /// The saga data type. public interface ISagaBuilder where TData : class, ISagaData { /// /// Adds a local step that executes synchronously within the orchestrator process. /// /// Unique name for this step. /// Builder for configuring the step. ISagaStepBuilder Step(string name); /// /// Adds a step that sends a command to a remote service via messaging. /// /// The command type to send. /// Unique name for this step. /// Builder for configuring the remote step. ISagaRemoteStepBuilder SendCommand(string name) where TCommand : class; /// /// Adds a step that sends a command and expects a specific result. /// /// The command type to send. /// The expected result type. /// Unique name for this step. /// Builder for configuring the remote step. ISagaRemoteStepBuilder SendCommand(string name) where TCommand : class; } /// /// Builder for configuring a local saga step. /// /// The saga data type. public interface ISagaStepBuilder where TData : class, ISagaData { /// /// Defines the execution action for this step. /// /// The action to execute. /// This builder for chaining. ISagaStepBuilder Execute(Func action); /// /// Defines the compensation action for this step. /// /// The compensation action to execute on rollback. /// This builder for chaining. ISagaStepBuilder Compensate(Func action); /// /// Completes this step definition and returns to the saga builder. /// /// The saga builder for adding more steps. ISagaBuilder Then(); } /// /// Builder for configuring a remote command saga step (no result). /// /// The saga data type. /// The command type to send. public interface ISagaRemoteStepBuilder where TData : class, ISagaData where TCommand : class { /// /// Defines how to build the command from saga data. /// /// Function to create the command. /// This builder for chaining. ISagaRemoteStepBuilder WithCommand(Func commandBuilder); /// /// Defines what to do when the command completes successfully. /// /// Handler for the response. /// This builder for chaining. ISagaRemoteStepBuilder OnResponse(Func handler); /// /// Defines the compensation command to send on rollback. /// /// The compensation command type. /// Function to create the compensation command. /// This builder for chaining. ISagaRemoteStepBuilder Compensate( Func compensationBuilder) where TCompensationCommand : class; /// /// Sets a timeout for this step. /// /// The timeout duration. /// This builder for chaining. ISagaRemoteStepBuilder WithTimeout(TimeSpan timeout); /// /// Configures retry behavior for this step. /// /// Maximum number of retries. /// Delay between retries. /// This builder for chaining. ISagaRemoteStepBuilder WithRetry(int maxRetries, TimeSpan delay); /// /// Completes this step definition and returns to the saga builder. /// /// The saga builder for adding more steps. ISagaBuilder Then(); } /// /// Builder for configuring a remote command saga step with result. /// /// The saga data type. /// The command type to send. /// The expected result type. public interface ISagaRemoteStepBuilder where TData : class, ISagaData where TCommand : class { /// /// Defines how to build the command from saga data. /// /// Function to create the command. /// This builder for chaining. ISagaRemoteStepBuilder WithCommand(Func commandBuilder); /// /// Defines what to do when the command completes successfully with a result. /// /// Handler for the response with result. /// This builder for chaining. ISagaRemoteStepBuilder OnResponse( Func handler); /// /// Defines the compensation command to send on rollback. /// /// The compensation command type. /// Function to create the compensation command. /// This builder for chaining. ISagaRemoteStepBuilder Compensate( Func compensationBuilder) where TCompensationCommand : class; /// /// Sets a timeout for this step. /// /// The timeout duration. /// This builder for chaining. ISagaRemoteStepBuilder WithTimeout(TimeSpan timeout); /// /// Configures retry behavior for this step. /// /// Maximum number of retries. /// Delay between retries. /// This builder for chaining. ISagaRemoteStepBuilder WithRetry(int maxRetries, TimeSpan delay); /// /// Completes this step definition and returns to the saga builder. /// /// The saga builder for adding more steps. ISagaBuilder Then(); }