/// <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);
        }
        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
        internal void Load(EventStoreSnapshot eventStoreSnapshot)
        {
            if (eventStoreSnapshot == null)
            {
                throw new ArgumentNullException(nameof(eventStoreSnapshot));
            }

            T body = eventStoreSnapshot.Body as T;

            if (body == null)
            {
                throw new EventSourcingException("Can't cast event type to given snapshot type.");
            }

            this.LoadFromSnapshot(body);
            this.EventStreamRevision             = eventStoreSnapshot.StreamRevision;
            this.NumberOfEventsSinceLastSnapshot = 0;
        }
예제 #4
0
 public abstract Task AddSnapshotAsync(string eventStreamId, string snapshotStreamId, EventStoreSnapshot eventStoreSnapshot);