private async Task <T> PublishAndRestore(
            Guid sourceId, CancellationToken cancellationToken)
        {
            await _eventPublisher
            .PublishPendingEvents <T>(sourceId, cancellationToken)
            .ConfigureAwait(false);

            IMemento memento = null;

            if (_mementoStore != null && _mementoEntityFactory != null)
            {
                memento = await _mementoStore
                          .Find <T>(sourceId, cancellationToken)
                          .ConfigureAwait(false);
            }

            IEnumerable <IDomainEvent> domainEvents = await _eventStore
                                                      .LoadEvents <T>(sourceId, memento?.Version ?? 0, cancellationToken)
                                                      .ConfigureAwait(false);

            return
                (memento == null
                ? domainEvents.Any()
                    ? _entityFactory.Invoke(sourceId, domainEvents)
                    : default(T)
                : _mementoEntityFactory.Invoke(sourceId, memento, domainEvents));
        }
        public static Task <IEnumerable <IDomainEvent> > LoadEvents <T>(
            this IAzureEventStore eventStore,
            Guid sourceId)
            where T : class, IEventSourced
        {
            if (eventStore == null)
            {
                throw new ArgumentNullException(nameof(eventStore));
            }

            return(eventStore.LoadEvents <T>(sourceId, default(int), CancellationToken.None));
        }