using System;
using System.Collections.Concurrent;
using Svrnty.CQRS.Events.Abstractions.Sagas;
namespace Svrnty.CQRS.Events.Sagas;
///
/// In-memory registry for saga definitions.
///
public sealed class SagaRegistry : ISagaRegistry
{
private readonly ConcurrentDictionary _definitionsByType = new();
private readonly ConcurrentDictionary _definitionsByName = new();
private readonly ConcurrentDictionary _typesByName = new();
///
public void Register(SagaDefinition definition) where TSaga : ISaga
{
if (definition == null)
throw new ArgumentNullException(nameof(definition));
var sagaType = typeof(TSaga);
_definitionsByType[sagaType] = definition;
_definitionsByName[definition.SagaName] = definition;
_typesByName[definition.SagaName] = sagaType;
}
///
public SagaDefinition? GetDefinition() where TSaga : ISaga
{
_definitionsByType.TryGetValue(typeof(TSaga), out var definition);
return definition;
}
///
public SagaDefinition? GetDefinitionByName(string sagaName)
{
if (string.IsNullOrWhiteSpace(sagaName))
return null;
_definitionsByName.TryGetValue(sagaName, out var definition);
return definition;
}
///
public Type? GetSagaType(string sagaName)
{
if (string.IsNullOrWhiteSpace(sagaName))
return null;
_typesByName.TryGetValue(sagaName, out var type);
return type;
}
}