55 lines
1.5 KiB
C#
55 lines
1.5 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 local saga steps.
|
|
/// </summary>
|
|
/// <typeparam name="TData">The saga data type.</typeparam>
|
|
public class LocalSagaStepBuilder<TData> : ISagaStepBuilder<TData>
|
|
where TData : class, ISagaData
|
|
{
|
|
private readonly SagaBuilder<TData> _parent;
|
|
private readonly LocalSagaStepDefinition<TData> _definition;
|
|
|
|
/// <summary>
|
|
/// Creates a new local 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 LocalSagaStepBuilder(SagaBuilder<TData> parent, string name, int order)
|
|
{
|
|
_parent = parent;
|
|
_definition = new LocalSagaStepDefinition<TData>
|
|
{
|
|
Name = name,
|
|
Order = order
|
|
};
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ISagaStepBuilder<TData> Execute(Func<TData, ISagaContext, CancellationToken, Task> action)
|
|
{
|
|
_definition.ExecuteAction = action;
|
|
return this;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ISagaStepBuilder<TData> Compensate(Func<TData, ISagaContext, CancellationToken, Task> action)
|
|
{
|
|
_definition.CompensateAction = action;
|
|
return this;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ISagaBuilder<TData> Then()
|
|
{
|
|
_parent.AddStep(_definition);
|
|
return _parent;
|
|
}
|
|
}
|