using BuildingBlocks.EventStoreDB.Serialization; using EventStore.Client; namespace BuildingBlocks.EventStoreDB.Events; public static class AggregateStreamExtensions { public static async Task AggregateStream( this EventStoreClient eventStore, Guid id, CancellationToken cancellationToken, ulong? fromVersion = null ) where T : class, IProjection { var readResult = eventStore.ReadStreamAsync( Direction.Forwards, StreamNameMapper.ToStreamId(id), fromVersion ?? StreamPosition.Start, cancellationToken: cancellationToken ); // TODO: consider adding extension method for the aggregation and deserialization var aggregate = (T)Activator.CreateInstance(typeof(T), true)!; if (await readResult.ReadState == ReadState.StreamNotFound) { return null; } await foreach (var @event in readResult) { var eventData = @event.Deserialize(); aggregate.When(eventData!); } return aggregate; } }