using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Svrnty.CQRS.Events.Abstractions.Projections;
namespace Svrnty.CQRS.Events.Projections;
///
/// In-memory registry for projection definitions.
///
///
/// Thread-safe implementation using ConcurrentDictionary.
///
public sealed class ProjectionRegistry : IProjectionRegistry
{
private readonly ConcurrentDictionary _projections = new();
///
public void Register(ProjectionDefinition definition)
{
if (definition == null)
throw new ArgumentNullException(nameof(definition));
if (string.IsNullOrWhiteSpace(definition.ProjectionName))
throw new ArgumentException("Projection name cannot be null or empty", nameof(definition));
if (!_projections.TryAdd(definition.ProjectionName, definition))
{
throw new InvalidOperationException(
$"A projection with name '{definition.ProjectionName}' is already registered");
}
}
///
public ProjectionDefinition? GetProjection(string projectionName)
{
return _projections.TryGetValue(projectionName, out var definition) ? definition : null;
}
///
public IEnumerable GetAllProjections()
{
return _projections.Values.ToList();
}
///
public IEnumerable GetProjectionsForStream(string streamName)
{
return _projections.Values.Where(p => p.StreamName == streamName).ToList();
}
}