namespace Prefab.Domain.Common;
///
/// Base implementation for aggregate entities that support domain events.
///
/// Entity identifier type.
public abstract class Entity : IEntity, IHasDomainEvents
{
private readonly List _events = [];
///
public T Id { get; set; } = default!;
///
public IReadOnlyCollection Events => _events.AsReadOnly();
///
public void AddEvent(Event e) => _events.Add(e);
///
public void RemoveEvent(Event e) => _events.Remove(e);
///
public void ClearEvents() => _events.Clear();
///
/// Generates a UUIDv7 identifier using the supplied timestamp, primarily for testing scenarios.
///
/// The timestamp used to seed the UUID generator.
/// Thrown when the entity identifier type is not .
protected void GenerateIdWithTimestamp(DateTime timestamp)
{
Id = typeof(T) == typeof(Guid)
? (T)(object)Guid.CreateVersion7(timestamp)
: throw new InvalidOperationException("Cannot generate a UUIDv7 for a non-Guid entity.");
}
}