コード例 #1
0
            public void Should_Invoke_Registered_Domain_Event_Handler()
            {
                TestDomainEventHandler handler = new TestDomainEventHandler(_testOutput);

                IEventSourcedAggregateRepository <TestAggregate, Guid> repository = Factory.CreateTestAggregateRepository(reg =>
                {
                    reg.Register <TestAggregateCreatedEvent>(() => handler);
                    reg.Register <OperationExecutedEvent>(() => handler);
                });

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

                aggregate.ExecuteSomeOperation("Test Operation");
                repository.Save(aggregate);

                // Event may not have yet been handled in background.
                Thread.Sleep(1000);

                // Aggregate should be stored.
                TestAggregate storedAggregate = repository.GetById(aggregate.Id);

                Assert.Equal(aggregate.Id, storedAggregate.Id);

                // Aggregate should have 2 events.
                // 1. TestAggregateCreated
                // 2. TestAggregateModified
                Assert.Equal(2, handler.HandledEvents.Count);
                Assert.Contains(handler.HandledEvents, (e) => e is TestAggregateCreatedEvent);
                Assert.Contains(handler.HandledEvents, (e) => e is OperationExecutedEvent);
            }
コード例 #2
0
            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);
            }