public async Task SaveAsync(TAggregate aggregateRoot, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Get a copy of the uncommited domain events before saving.
            IDomainEventStream <TAggregateId> domainEventStreamToSave = aggregateRoot.GetUncommitedDomainEvents();

            await _domainEventStore.SaveAsync(aggregateRoot, cancellationToken).ConfigureAwait(false);

            // No need to await. Any publish errors will be communicated through OnError event.
            // Not passing cancellation token since event notification should not be cancelled.
            Task publishTask = PublishDomainEventsAsync(domainEventStreamToSave);
        }
            public async Task Should_Append_To_Domain_Event_Store()
            {
                IDomainEventAsyncStore <TestAggregate, Guid> eventStore = Factory.CreateEventAsyncStore <TestAggregate, Guid>();

                // Create aggregate.
                TestAggregate aggregate = new TestAggregate(Guid.NewGuid());
                await eventStore.SaveAsync(aggregate);

                IDomainEventStream <Guid> stream = await eventStore.GetDomainEventStreamAsync(aggregate.Id);

                Assert.NotNull(stream);
                Assert.Equal(aggregate.Id, stream.AggregateId);
                Assert.Equal(1, stream.DomainEventCount);
            }
            public async Task GetDomainEventStreamAsync_Should_Retrieve_Aggregate_Stream()
            {
                IDomainEventAsyncStore <TestAggregate, Guid> eventStore = Factory.CreateEventAsyncStore <TestAggregate, Guid>();

                // Create and modify aggregate.
                TestAggregate aggregate = new TestAggregate(Guid.NewGuid());

                aggregate.ExecuteSomeOperation("I was modified!~");
                await eventStore.SaveAsync(aggregate);

                IDomainEventStream <Guid> stream = await eventStore.GetDomainEventStreamAsync(aggregate.Id);

                Assert.NotNull(stream);
                Assert.Equal(aggregate.Id, stream.AggregateId);

                // 2 domain events in total: Created + Modified events.
                Assert.Equal(2, stream.DomainEventCount);

                // Stream starts from version 1 to 2.
                Assert.Equal(1, stream.BeginVersion);
                Assert.Equal(2, stream.EndVersion);
            }