dotnet-cqrs/Svrnty.CQRS.Events/Configuration/StreamConfiguration.cs

86 lines
2.9 KiB
C#

using System;
using Svrnty.CQRS.Events.Abstractions.Delivery;
using Svrnty.CQRS.Events.Abstractions.Streaming;
using Svrnty.CQRS.Events.Abstractions.Configuration;
using Svrnty.CQRS.Events.Abstractions;
namespace Svrnty.CQRS.Events.Configuration;
/// <summary>
/// Default implementation of <see cref="IStreamConfiguration"/>.
/// Provides configuration for an event stream's storage, delivery, and retention behavior.
/// </summary>
public class StreamConfiguration : IStreamConfiguration
{
/// <summary>
/// Initializes a new instance of the <see cref="StreamConfiguration"/> class.
/// </summary>
/// <param name="streamName">Name of the stream. Must not be null or whitespace.</param>
/// <exception cref="ArgumentException">Thrown if <paramref name="streamName"/> is null or whitespace.</exception>
public StreamConfiguration(string streamName)
{
if (string.IsNullOrWhiteSpace(streamName))
throw new ArgumentException("Stream name cannot be null or whitespace.", nameof(streamName));
StreamName = streamName;
// Set defaults
Type = StreamType.Ephemeral;
DeliverySemantics = DeliverySemantics.AtLeastOnce;
Scope = StreamScope.Internal;
Retention = null;
EnableReplay = false;
}
/// <inheritdoc />
public string StreamName { get; }
/// <inheritdoc />
public StreamType Type { get; set; }
/// <inheritdoc />
public DeliverySemantics DeliverySemantics { get; set; }
/// <inheritdoc />
public StreamScope Scope { get; set; }
/// <inheritdoc />
public TimeSpan? Retention { get; set; }
/// <inheritdoc />
public bool EnableReplay { get; set; }
/// <summary>
/// Validates the configuration settings.
/// </summary>
/// <exception cref="InvalidOperationException">
/// Thrown if configuration is invalid (e.g., ephemeral stream with replay enabled).
/// </exception>
public void Validate()
{
// Ephemeral streams cannot have replay enabled
if (Type == StreamType.Ephemeral && EnableReplay)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Ephemeral streams cannot have replay enabled. " +
"Set Type = StreamType.Persistent to enable replay.");
}
// Ephemeral streams shouldn't have retention policies
if (Type == StreamType.Ephemeral && Retention.HasValue)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Ephemeral streams do not support retention policies. " +
"Retention only applies to persistent streams.");
}
// Retention must be positive if set
if (Retention.HasValue && Retention.Value <= TimeSpan.Zero)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Retention period must be positive. " +
$"Got: {Retention.Value}");
}
}
}