コード例 #1
0
        /// <summary>
        /// Loads the aggregate using the latest snapshot and then applies all events
        /// in the event stream from the stream revision that the snapshot was taken.
        /// </summary>
        /// <typeparam name="T">Snapshot type</typeparam>
        /// <param name="aggregate">The aggregate.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task LoadFromLatestSnapshotAsync <T>(IAggregateWithSnapshot <T> aggregate)
            where T : class
        {
            AggregateWithSnapshot <T> baseAggregate = aggregate as AggregateWithSnapshot <T>;

            if (baseAggregate == null)
            {
                string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "The supplied Aggregate did not inherit from {0}.", typeof(Aggregate).FullName);
                throw new EventSourcingException(exceptionMessage);
            }

            // Load from the snapshot.
            EventStoreSnapshot eventStoreSnapshot = await this.eventStoreProvider.ReadSnapshotAsync(aggregate.SnapshotStreamId).ConfigureAwait(false);

            baseAggregate.Load(eventStoreSnapshot);

            // Now load all events since the snapshot was taken.
            IEventStoreStream eventStoreStream =
                await this.eventStoreProvider.ReadEventsAsync(
                    aggregate.EventStreamId,
                    eventStoreSnapshot.StreamRevision,
                    ExpectedStreamRevision.End).ConfigureAwait(false);

            baseAggregate.Load(eventStoreStream);
        }
コード例 #2
0
        public async Task SaveSnapshotAsync <T>(IAggregateWithSnapshot <T> aggregate, Guid snapshotId)
            where T : class
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            T snapshot = aggregate.TakeSnapshot();
            EventStoreSnapshot eventStoreSnapshot = new EventStoreSnapshot(snapshotId, aggregate.EventStreamRevision, snapshot);

            await this.eventStoreProvider.AddSnapshotAsync(aggregate.EventStreamId, aggregate.SnapshotStreamId, eventStoreSnapshot).ConfigureAwait(false);

            aggregate.NumberOfEventsSinceLastSnapshot = 0;
        }
コード例 #3
0
        public async Task LoadFromLatestSnapshotIfExistsAsync <T>(IAggregateWithSnapshot <T> aggregate)
            where T : class
        {
            AggregateWithSnapshot <T> baseAggregate = aggregate as AggregateWithSnapshot <T>;

            if (baseAggregate == null)
            {
                string exceptionMessage = string.Format(CultureInfo.InvariantCulture, "The supplied Aggregate did not inherit from {0}.", typeof(Aggregate).FullName);
                throw new EventSourcingException(exceptionMessage);
            }

            bool snapshotExists = await this.eventStoreProvider.CheckSnapshotExistsAsync <T>(aggregate.SnapshotStreamId).ConfigureAwait(false);

            if (snapshotExists)
            {
                await this.LoadFromLatestSnapshotAsync(aggregate).ConfigureAwait(false);
            }
            else
            {
                await this.LoadIfExistsAsync(aggregate).ConfigureAwait(false);
            }
        }
コード例 #4
0
 public async Task SaveSnapshotAsync <T>(IAggregateWithSnapshot <T> aggregate)
     where T : class
 {
     await this.SaveSnapshotAsync(aggregate, Guid.NewGuid()).ConfigureAwait(false);
 }