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