using System;
using System.Collections.Generic;
using Svrnty.CQRS.Sagas.Abstractions;
namespace Svrnty.CQRS.Sagas;
///
/// Implementation of saga context providing runtime information during step execution.
///
public class SagaContext : ISagaContext
{
private readonly SagaState _state;
///
/// Creates a new saga context from a saga state.
///
/// The saga state.
public SagaContext(SagaState state)
{
_state = state ?? throw new ArgumentNullException(nameof(state));
}
///
public Guid SagaId => _state.SagaId;
///
public Guid CorrelationId => _state.CorrelationId;
///
public string SagaType => _state.SagaType;
///
public int CurrentStepIndex => _state.CurrentStepIndex;
///
public string CurrentStepName => _state.CurrentStepName ?? string.Empty;
///
public IReadOnlyDictionary StepResults => _state.StepResults;
///
public T? GetStepResult(string stepName)
{
if (_state.StepResults.TryGetValue(stepName, out var value) && value is T result)
{
return result;
}
return default;
}
///
public void SetStepResult(T result)
{
_state.StepResults[CurrentStepName] = result;
}
}