50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Svrnty.CQRS.Sagas.Abstractions;
|
|
|
|
namespace Svrnty.CQRS.Sagas.Builders;
|
|
|
|
/// <summary>
|
|
/// Implementation of the saga builder for defining saga steps.
|
|
/// </summary>
|
|
/// <typeparam name="TData">The saga data type.</typeparam>
|
|
public class SagaBuilder<TData> : ISagaBuilder<TData>
|
|
where TData : class, ISagaData
|
|
{
|
|
private readonly List<SagaStepDefinition> _steps = new();
|
|
|
|
/// <summary>
|
|
/// Gets the defined steps.
|
|
/// </summary>
|
|
public IReadOnlyList<SagaStepDefinition> Steps => _steps.AsReadOnly();
|
|
|
|
/// <inheritdoc />
|
|
public ISagaStepBuilder<TData> Step(string name)
|
|
{
|
|
return new LocalSagaStepBuilder<TData>(this, name, _steps.Count);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ISagaRemoteStepBuilder<TData, TCommand> SendCommand<TCommand>(string name)
|
|
where TCommand : class
|
|
{
|
|
return new RemoteSagaStepBuilder<TData, TCommand>(this, name, _steps.Count);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public ISagaRemoteStepBuilder<TData, TCommand, TResult> SendCommand<TCommand, TResult>(string name)
|
|
where TCommand : class
|
|
{
|
|
return new RemoteSagaStepBuilderWithResult<TData, TCommand, TResult>(this, name, _steps.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a step definition to the builder.
|
|
/// </summary>
|
|
/// <param name="step">The step definition to add.</param>
|
|
internal void AddStep(SagaStepDefinition step)
|
|
{
|
|
_steps.Add(step);
|
|
}
|
|
}
|