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));
        }
 private Task <IMemento> FindMemento(Guid sourceId, CancellationToken cancellationToken)
 {
     return(_mementoStore.Find <T>(sourceId, cancellationToken));
 }