Пример #1
0
        protected virtual async Task <TAggregate> FindAggregateAsync(TKey id,
                                                                     bool ignoreSnapshot = false,
                                                                     int version         = -1,
                                                                     CancellationToken cancellationToken = default)
        {
            int maxVersion = version <= 0 ? int.MaxValue : version;

            IAggregateSnapshot <TKey> snapshot = null;

            if (!ignoreSnapshot)
            {
                snapshot = await _snapshotStore
                           .FindLastSnapshotAsync(id, maxVersion, cancellationToken)
                           .ConfigureAwait(false);
            }

            int minVersion = snapshot == null ? 1 : snapshot.AggregateVersion + 1;

            IAggregateEvent <TKey>[] events = await _eventStore
                                              .GetEventsAsync(id, minVersion, maxVersion, cancellationToken)
                                              .ConfigureAwait(false);

            if (snapshot == null)
            {
                return(events.Length == 0
                    ? null
                    : Activator.CreateInstance(typeof(TAggregate), id, events as IEnumerable <IAggregateEvent <TKey> >) as TAggregate);
            }
            else
            {
                return(Activator.CreateInstance(typeof(TAggregate), id, snapshot, events as IEnumerable <IAggregateEvent <TKey> >) as TAggregate);
            }
        }