private async Task CommitEventsAsync(FilesEventPersistence persistence) { var events = Enumerable.Range(0, NumberOfEventsPerBatch) .Select(i => new ThingyPingEvent(PingId.New)) .ToList(); await RetryAsync(async() => { var version = await GetVersionAsync(persistence).ConfigureAwait(false); var serializedEvents = from aggregateEvent in events let metadata = new Metadata { AggregateSequenceNumber = ++version } let serializedEvent = _serializer.Serialize(aggregateEvent, metadata) select serializedEvent; var readOnlyEvents = new ReadOnlyCollection <SerializedEvent>(serializedEvents.ToList()); await persistence .CommitEventsAsync(ThingyId, readOnlyEvents, CancellationToken.None) .ConfigureAwait(false); }) .ConfigureAwait(false); }
private static async Task <int> GetVersionAsync(FilesEventPersistence persistence) { var existingEvents = await persistence.LoadCommittedEventsAsync( ThingyId, 1, CancellationToken.None).ConfigureAwait(false); int version = existingEvents.LastOrDefault()?.AggregateSequenceNumber ?? 0; return(version); }