Example #1
0
        /// <summary>
        /// Checks that if the given aggregate root has been updated in the event store by another process.
        /// </summary>
        /// <param name="aggregateRoot">The aggregate root.</param>
        /// <returns></returns>
        /// <exception cref="AggregateConcurrencyException"></exception>
        protected async Task ConcurrencyCheck(TAggregateRoot aggregateRoot)
        {
            IDocumentQuery <TEventSet> eventQuery = Client
                                                    .CreateDocumentQuery <TEventSet>(_databaseCollectionUri)
                                                    .Where(eventSet => eventSet.AggregateId == aggregateRoot.Id)
                                                    .AsDocumentQuery();

            FeedResponse <TEventSet> result = await eventQuery.ExecuteNextAsync <TEventSet>();

            int persistedVersion = result.Max(eventSet => (int?)eventSet.AggregateVersion) ?? 0;

            if (aggregateRoot.Version != persistedVersion + 1)
            {
                string errorMessage =
                    $"The aggregate with aggregate root of type {aggregateRoot.GetType().Name} has been modified and the event stream cannot be appended.";
                throw new AggregateConcurrencyException(errorMessage);
            }
        }