public async Task <IReadOnlyCollection <ICommittedDomainEvent> > CommitEventsAsync(IIdentity id, IReadOnlyCollection <SerializedEvent> serializedEvents, CancellationToken cancellationToken)
        {
            if (!serializedEvents.Any())
            {
                return(new ICommittedDomainEvent[] { });
            }

            var eventDataModels = serializedEvents
                                  .Select((e, i) => new MongoDbEventDataModel
            {
                _id                     = _mongoDbEventSequenceStore.GetNextSequence(_collectionName),
                AggregateId             = id.Value,
                AggregateName           = e.Metadata[MetadataKeys.AggregateName],
                BatchId                 = Guid.Parse(e.Metadata[MetadataKeys.BatchId]),
                Data                    = e.SerializedData,
                Metadata                = e.SerializedMetadata,
                AggregateSequenceNumber = e.AggregateSequenceNumber
            })
                                  .OrderBy(x => x.AggregateSequenceNumber)
                                  .ToList();

            _log.Verbose("Committing {0} events to MongoDb event store for entity with ID '{1}'", eventDataModels.Count, id);
            await MongoDbEventStoreCollection
            .InsertManyAsync(eventDataModels, cancellationToken : cancellationToken)
            .ConfigureAwait(continueOnCapturedContext: false);

            return(eventDataModels);
        }
        public async Task <IReadOnlyCollection <ICommittedDomainEvent> > CommitEventsAsync(IIdentity id, IReadOnlyCollection <SerializedEvent> serializedEvents, CancellationToken cancellationToken)
        {
            if (!serializedEvents.Any())
            {
                return(Array.Empty <ICommittedDomainEvent>());
            }

            var eventDataModels = serializedEvents
                                  .Select((e, i) => new MongoDbEventDataModel
            {
                _id                     = DateTime.UtcNow.Ticks,
                AggregateId             = id.Value,
                AggregateName           = e.Metadata[MetadataKeys.AggregateName],
                BatchId                 = Guid.Parse(e.Metadata[MetadataKeys.BatchId]),
                Data                    = e.SerializedData,
                Metadata                = e.SerializedMetadata,
                AggregateSequenceNumber = e.AggregateSequenceNumber
            })
                                  .OrderBy(x => x.AggregateSequenceNumber)
                                  .ToList();

            _log.Verbose("Committing {0} events to MongoDb event store for entity with ID '{1}'", eventDataModels.Count, id);
            try
            {
                await MongoDbEventStoreCollection
                .InsertManyAsync(eventDataModels, cancellationToken : cancellationToken)
                .ConfigureAwait(continueOnCapturedContext: false);
            }
            catch (MongoBulkWriteException e)
            {
                throw new OptimisticConcurrencyException(e.Message, e);
            }
            return(eventDataModels);
        }