namespace MS.Microservice.Domain.EventSourcing; public interface IEventSourcedEvent; public sealed record EventMetadata( string? CorrelationId = null, string? CausationId = null, string? UserId = null, string? TenantId = null, string? TraceId = null, int SchemaVersion = 1); public sealed record EventEnvelope( Guid EventId, string StreamId, string StreamType, int Version, long GlobalPosition, TEvent Data, EventMetadata Metadata, DateTimeOffset CreatedAt) where TEvent : class, IEventSourcedEvent; public sealed record AggregateSnapshot( string StreamId, string StreamType, int Version, TState State, DateTimeOffset CreatedAt); public interface IEventStore { Task>> LoadStreamAsync( string streamId, int fromVersion = 1, CancellationToken cancellationToken = default) where TEvent : class, IEventSourcedEvent; Task>> ReadAllAsync( long afterGlobalPosition, string? streamType = null, CancellationToken cancellationToken = default) where TEvent : class, IEventSourcedEvent; Task AppendToStreamAsync( string streamId, string streamType, int expectedVersion, IReadOnlyCollection events, Func? metadataFactory = null, CancellationToken cancellationToken = default) where TEvent : class, IEventSourcedEvent; } public interface ISnapshotStore { Task?> GetLatestAsync( string streamId, CancellationToken cancellationToken = default); Task UpsertAsync( AggregateSnapshot snapshot, CancellationToken cancellationToken = default); } public interface IProjectionCheckpointStore { Task GetLastPositionAsync( string projectionName, CancellationToken cancellationToken = default); Task StoreAsync( string projectionName, long lastGlobalPosition, CancellationToken cancellationToken = default); } public sealed class EventStoreConcurrencyException : Exception { public EventStoreConcurrencyException(string streamId, int expectedVersion, int actualVersion) : base($"事件流 {streamId} 发生并发冲突,期望版本 {expectedVersion},实际版本 {actualVersion}。") { StreamId = streamId; ExpectedVersion = expectedVersion; ActualVersion = actualVersion; } public EventStoreConcurrencyException( string streamId, int expectedVersion, int actualVersion, Exception innerException) : base($"事件流 {streamId} 发生并发冲突,期望版本 {expectedVersion},实际版本 {actualVersion}。", innerException) { StreamId = streamId; ExpectedVersion = expectedVersion; ActualVersion = actualVersion; } public string StreamId { get; } public int ExpectedVersion { get; } public int ActualVersion { get; } }