dotnet-cqrs/Svrnty.CQRS.Events.Abstractions/Configuration/LifecycleConfiguration.cs

74 lines
2.5 KiB
C#

using System;
namespace Svrnty.CQRS.Events.Abstractions.Configuration;
/// <summary>
/// Configuration for stream lifecycle management.
/// </summary>
public class LifecycleConfiguration
{
/// <summary>
/// Gets or sets whether to automatically create the stream if it doesn't exist.
/// </summary>
public bool AutoCreate { get; set; } = true;
/// <summary>
/// Gets or sets whether to automatically archive old events.
/// </summary>
public bool AutoArchive { get; set; }
/// <summary>
/// Gets or sets the age after which events should be archived.
/// </summary>
public TimeSpan? ArchiveAfter { get; set; }
/// <summary>
/// Gets or sets the location where archived events should be stored.
/// </summary>
public string? ArchiveLocation { get; set; }
/// <summary>
/// Gets or sets whether to automatically delete old events.
/// </summary>
public bool AutoDelete { get; set; }
/// <summary>
/// Gets or sets the age after which events should be deleted.
/// </summary>
public TimeSpan? DeleteAfter { get; set; }
/// <summary>
/// Validates the lifecycle configuration.
/// </summary>
/// <exception cref="ArgumentException">Thrown when configuration is invalid.</exception>
public void Validate()
{
if (AutoArchive)
{
if (!ArchiveAfter.HasValue)
throw new ArgumentException("ArchiveAfter must be specified when AutoArchive is enabled", nameof(ArchiveAfter));
if (ArchiveAfter.Value <= TimeSpan.Zero)
throw new ArgumentException("ArchiveAfter must be positive", nameof(ArchiveAfter));
if (string.IsNullOrWhiteSpace(ArchiveLocation))
throw new ArgumentException("ArchiveLocation must be specified when AutoArchive is enabled", nameof(ArchiveLocation));
}
if (AutoDelete)
{
if (!DeleteAfter.HasValue)
throw new ArgumentException("DeleteAfter must be specified when AutoDelete is enabled", nameof(DeleteAfter));
if (DeleteAfter.Value <= TimeSpan.Zero)
throw new ArgumentException("DeleteAfter must be positive", nameof(DeleteAfter));
}
if (AutoArchive && AutoDelete && ArchiveAfter.HasValue && DeleteAfter.HasValue)
{
if (DeleteAfter.Value <= ArchiveAfter.Value)
throw new ArgumentException("DeleteAfter must be greater than ArchiveAfter", nameof(DeleteAfter));
}
}
}