dotnet-cqrs/Svrnty.CQRS.Events/Projections/ProjectionRegistry.cs

53 lines
1.6 KiB
C#

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Svrnty.CQRS.Events.Abstractions.Projections;
namespace Svrnty.CQRS.Events.Projections;
/// <summary>
/// In-memory registry for projection definitions.
/// </summary>
/// <remarks>
/// Thread-safe implementation using ConcurrentDictionary.
/// </remarks>
public sealed class ProjectionRegistry : IProjectionRegistry
{
private readonly ConcurrentDictionary<string, ProjectionDefinition> _projections = new();
/// <inheritdoc />
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");
}
}
/// <inheritdoc />
public ProjectionDefinition? GetProjection(string projectionName)
{
return _projections.TryGetValue(projectionName, out var definition) ? definition : null;
}
/// <inheritdoc />
public IEnumerable<ProjectionDefinition> GetAllProjections()
{
return _projections.Values.ToList();
}
/// <inheritdoc />
public IEnumerable<ProjectionDefinition> GetProjectionsForStream(string streamName)
{
return _projections.Values.Where(p => p.StreamName == streamName).ToList();
}
}