예제 #1
0
        protected virtual async Task <IAggregateChangeset <TKey> > SaveAggregateAsync(TAggregate aggregate,
                                                                                      CancellationToken cancellationToken = default)
        {
            if (aggregate is null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            IAggregateChangeset <TKey> changeset = aggregate.GetChangeset();

            foreach (IAggregateEvent <TKey> @event in changeset.Events)
            {
                await _eventStore.AddEventAsync(@event, cancellationToken).ConfigureAwait(false);
            }

            if (changeset.Snapshot != null)
            {
                await _snapshotStore.AddSnapshotAsync(changeset.Snapshot, cancellationToken).ConfigureAwait(false);
            }

            changeset.Commit();
            return(changeset);
        }