private static async Task TakeSnapshot(IEventStoreConnection connection, IAggregateRoot aggregate) { var snapshot = aggregate.TakeSnapshot(); var encodedEvent = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(snapshot)); var eventData = new EventData(Guid.NewGuid(), snapshot.GetType().Name, true, encodedEvent, null); await connection.AppendToStreamAsync(StreamNameGenerator.GenerateForSnapshot(aggregate), ExpectedVersion.Any, eventData); }
private async Task SaveAggregatesEvents() { foreach (var aggregate in _trackedAggregates) { using (var transaction = await _context.StartTransactionAsync(StreamNameGenerator.Generate(aggregate), aggregate.CurrentVersion)) { await transaction.WriteAsync(aggregate.GetUnCommittedChanges().ToEventData()); await transaction.CommitAsync(); await _snapshotter.TakeSnapshotIfNeed(aggregate); } } }