/// <summary>
        /// Persist aggregate to the event store and publish the events.
        /// </summary>
        /// <param name="aggregateRoot">Aggregate to persist.</param>
        public void Save(TAggregate aggregateRoot)
        {
            // Get a copy of the uncommited domain events before saving.
            IDomainEventStream <TAggregateId> domainEventStreamToSave = aggregateRoot.GetUncommitedDomainEvents();

            _domainEventStore.Save(aggregateRoot);

            // 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);
        }
Example #2
0
        public void Save <T>(T aggregate, SessionInfo sessionInfo, int?expectedVersion = null) where T : AggregateRoot
        {
            if (expectedVersion != null && _domainEventStore.Get(aggregate.Id, expectedVersion.Value).Any())
            {
                throw new ConcurrencyException(aggregate.Id);
            }

            var changes = aggregate.FlushUncommitedChanges().ToArray();

            if (changes.Any())
            {
                _domainEventStore.Save(changes, sessionInfo);
                _domainEventPublisher.Publish(changes, sessionInfo.TrackingId);
            }
        }
            public void Should_Append_To_Domain_Event_Store()
            {
                IDomainEventStore <TestAggregate, Guid> eventStore = Factory.CreateEventStore <TestAggregate, Guid>();

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

                eventStore.Save(aggregate);

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

                var fromStream = new TestAggregate(stream);

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

                // 1 domain event in total: Created event.
                Assert.Equal(1, stream.DomainEventCount);
            }
            public void Should_Retrieve_Aggregate_Stream()
            {
                IDomainEventStore <TestAggregate, Guid> eventStore = Factory.CreateEventStore <TestAggregate, Guid>();

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

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

                IDomainEventStream <Guid> stream = eventStore.GetDomainEventStream(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);
            }