using System;
using System.Threading;
using System.Threading.Tasks;
using Svrnty.CQRS.Sagas.Abstractions;
namespace Svrnty.CQRS.Sagas.Builders;
///
/// Base class for saga step definitions.
///
public abstract class SagaStepDefinition
{
///
/// Unique name for this step.
///
public string Name { get; set; } = string.Empty;
///
/// Order of the step in the saga.
///
public int Order { get; set; }
///
/// Whether this step has a compensation action.
///
public abstract bool HasCompensation { get; }
///
/// Whether this step is a remote step (sends a command).
///
public abstract bool IsRemote { get; }
///
/// Timeout for this step.
///
public TimeSpan? Timeout { get; set; }
///
/// Maximum number of retries.
///
public int MaxRetries { get; set; }
///
/// Delay between retries.
///
public TimeSpan RetryDelay { get; set; } = TimeSpan.FromSeconds(1);
}
///
/// Definition for a local saga step.
///
/// The saga data type.
public class LocalSagaStepDefinition : SagaStepDefinition
where TData : class, ISagaData
{
///
/// The execution action.
///
public Func? ExecuteAction { get; set; }
///
/// The compensation action.
///
public Func? CompensateAction { get; set; }
///
public override bool HasCompensation => CompensateAction != null;
///
public override bool IsRemote => false;
}
///
/// Definition for a remote saga step.
///
/// The saga data type.
/// The command type.
public class RemoteSagaStepDefinition : SagaStepDefinition
where TData : class, ISagaData
where TCommand : class
{
///
/// Function to build the command.
///
public Func? CommandBuilder { get; set; }
///
/// Handler for successful response.
///
public Func? ResponseHandler { get; set; }
///
/// Type of the compensation command.
///
public Type? CompensationCommandType { get; set; }
///
/// Function to build the compensation command.
///
public Func? CompensationBuilder { get; set; }
///
public override bool HasCompensation => CompensationBuilder != null;
///
public override bool IsRemote => true;
}
///
/// Definition for a remote saga step with result.
///
/// The saga data type.
/// The command type.
/// The result type.
public class RemoteSagaStepDefinition : SagaStepDefinition
where TData : class, ISagaData
where TCommand : class
{
///
/// Function to build the command.
///
public Func? CommandBuilder { get; set; }
///
/// Handler for successful response with result.
///
public Func? ResponseHandler { get; set; }
///
/// Type of the compensation command.
///
public Type? CompensationCommandType { get; set; }
///
/// Function to build the compensation command.
///
public Func? CompensationBuilder { get; set; }
///
/// The expected result type.
///
public Type ResultType => typeof(TResult);
///
public override bool HasCompensation => CompensationBuilder != null;
///
public override bool IsRemote => true;
}