using System;
using System.Threading;
using System.Threading.Tasks;
using Svrnty.CQRS.Sagas.Abstractions;
namespace Svrnty.CQRS.Sagas.Builders;
///
/// Builder for configuring remote saga steps (without result).
///
/// The saga data type.
/// The command type.
public class RemoteSagaStepBuilder : ISagaRemoteStepBuilder
where TData : class, ISagaData
where TCommand : class
{
private readonly SagaBuilder _parent;
private readonly RemoteSagaStepDefinition _definition;
///
/// Creates a new remote step builder.
///
/// The parent saga builder.
/// The step name.
/// The step order.
public RemoteSagaStepBuilder(SagaBuilder parent, string name, int order)
{
_parent = parent;
_definition = new RemoteSagaStepDefinition
{
Name = name,
Order = order
};
}
///
public ISagaRemoteStepBuilder WithCommand(Func commandBuilder)
{
_definition.CommandBuilder = commandBuilder;
return this;
}
///
public ISagaRemoteStepBuilder OnResponse(Func handler)
{
_definition.ResponseHandler = handler;
return this;
}
///
public ISagaRemoteStepBuilder Compensate(
Func compensationBuilder)
where TCompensationCommand : class
{
_definition.CompensationCommandType = typeof(TCompensationCommand);
_definition.CompensationBuilder = (data, ctx) => compensationBuilder(data, ctx);
return this;
}
///
public ISagaRemoteStepBuilder WithTimeout(TimeSpan timeout)
{
_definition.Timeout = timeout;
return this;
}
///
public ISagaRemoteStepBuilder WithRetry(int maxRetries, TimeSpan delay)
{
_definition.MaxRetries = maxRetries;
_definition.RetryDelay = delay;
return this;
}
///
public ISagaBuilder Then()
{
_parent.AddStep(_definition);
return _parent;
}
}
///
/// Builder for configuring remote saga steps with result.
///
/// The saga data type.
/// The command type.
/// The result type.
public class RemoteSagaStepBuilderWithResult : ISagaRemoteStepBuilder
where TData : class, ISagaData
where TCommand : class
{
private readonly SagaBuilder _parent;
private readonly RemoteSagaStepDefinition _definition;
///
/// Creates a new remote step builder with result.
///
/// The parent saga builder.
/// The step name.
/// The step order.
public RemoteSagaStepBuilderWithResult(SagaBuilder parent, string name, int order)
{
_parent = parent;
_definition = new RemoteSagaStepDefinition
{
Name = name,
Order = order
};
}
///
public ISagaRemoteStepBuilder WithCommand(Func commandBuilder)
{
_definition.CommandBuilder = commandBuilder;
return this;
}
///
public ISagaRemoteStepBuilder OnResponse(
Func handler)
{
_definition.ResponseHandler = handler;
return this;
}
///
public ISagaRemoteStepBuilder Compensate(
Func compensationBuilder)
where TCompensationCommand : class
{
_definition.CompensationCommandType = typeof(TCompensationCommand);
_definition.CompensationBuilder = (data, ctx) => compensationBuilder(data, ctx);
return this;
}
///
public ISagaRemoteStepBuilder WithTimeout(TimeSpan timeout)
{
_definition.Timeout = timeout;
return this;
}
///
public ISagaRemoteStepBuilder WithRetry(int maxRetries, TimeSpan delay)
{
_definition.MaxRetries = maxRetries;
_definition.RetryDelay = delay;
return this;
}
///
public ISagaBuilder Then()
{
_parent.AddStep(_definition);
return _parent;
}
}