public async Task SaveAggregate(TAggregateRoot aggregate)
        {
            if (!aggregate.HasChanges())
            {
                return;
            }

            var streamId = GetStreamId(aggregate.AggregateId);
            var changes  = aggregate.GetChanges();

            if (aggregate.AggregateIsNotPersisted())
            {
                await _eventStore.CreateStream(streamId, changes);
            }
            else
            {
                await _eventStore.AppendStream(streamId, changes, aggregate.AggregateVersion);
            }
        }
        private IEventStream GetStream(Guid streamId, bool createIfNotExists)
        {
            IEventStream stream;

            if (_streams.TryGetValue(streamId, out stream))
            {
                return(stream);
            }

            stream = _eventStore.OpenStream(streamId);
            if (stream != null)
            {
                stream = _streams.GetOrAdd(streamId, stream);
                return(stream);
            }

            if (createIfNotExists)
            {
                stream = _streams.GetOrAdd(streamId, id => _eventStore.CreateStream(id));
                return(stream);
            }

            return(null);
        }