コード例 #1
0
        private async Task CommitChangesAsync(Aggregate aggregate)
        {
            var expectedVersion = aggregate.LastCommittedVersion;

            var item = await _eventStorageProvider.GetLastEventAsync(aggregate.GetType(), aggregate.Id)
                       .ConfigureAwait(false);

            if (item != null && expectedVersion == (int)Aggregate.StreamState.NoStream)
            {
                throw new AggregateCreationException(aggregate.Id, item.TargetVersion + 1);
            }
            if (item != null && item.TargetVersion + 1 != expectedVersion)
            {
                throw new ConcurrencyException(aggregate.Id);
            }

            var changesToCommit = aggregate.GetUncommittedChanges().ToList();

            Logger.Debug("Performing pre commit checks");

            //perform pre commit actions
            foreach (var e in changesToCommit)
            {
                DoPreCommitTasks(e);
            }

            //CommitAsync events to storage provider
            await _eventStorageProvider.CommitChangesAsync(aggregate)
            .ConfigureAwait(false);

            //Publish to event publisher asynchronously
            if (_eventPublisher != null)
            {
                foreach (var e in changesToCommit)
                {
                    await _eventPublisher.PublishAsync(e)
                    .ConfigureAwait(false);
                }
            }

            //If the Aggregate implements snapshottable
            var snapshottable = aggregate as ISnapshottable;

            if (snapshottable != null && _snapshotStorageProvider != null)
            {
                //Every N events we save a snapshot
                if (ShouldCreateSnapShot(aggregate, changesToCommit))
                {
                    await _snapshotStorageProvider.SaveSnapshotAsync(aggregate.GetType(), snapshottable.TakeSnapshot())
                    .ConfigureAwait(false);
                }
            }

            aggregate.MarkChangesAsCommitted();
        }
コード例 #2
0
        private async Task CommitChangesAsync(Aggregate aggregate)
        {
            var changesToCommit = aggregate.GetUncommittedChanges().ToList();

            foreach (var e in changesToCommit)
            {
                e.EventCommittedTimestamp = Clock.Now();
            }

            await _provider.CommitChangesAsync(aggregate).ConfigureAwait(false);

            //as we are not using the repo we need to remember to do this
            aggregate.MarkChangesAsCommitted();
        }
コード例 #3
0
 public Task CommitChangesAsync(Aggregate aggregate)
 {
     return(LogMethodCallAsync(() => _decorated.CommitChangesAsync(aggregate), aggregate));
 }