Esempio n. 1
0
        private static TestAggregate GetAggregateWithEvents(int numberOfEventsBetweenSnapshots, int numberOfEvents)
        {
            TestAggregate testAggregate = new TestAggregate(
                Guid.NewGuid().ToEventStreamIdFormattedString(),
                numberOfEventsBetweenSnapshots);

            for (int i = 0; i < numberOfEvents; i++)
            {
                testAggregate.BusinessLogicThatResultsInEventA("value");
            }

            return(testAggregate);
        }
Esempio n. 2
0
        public async Task Given_No_Uncommitted_Events_For_The_Aggregate_When_Save_Is_Called_Then_An_Error_Is_Raised()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            // Assert
            TestAggregate testAggregate = new TestAggregate(eventStreamId);
            await AssertExtensions.ThrowsAsync <EventSourcingException>(
                async() => await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false)).ConfigureAwait(false);
        }
Esempio n. 3
0
        public void Given_An_Event_And_Matching_UpdateState_Method_When_The_Event_Is_Applied_Then_The_Uncommitted_Event_Is_The_Correct_Type()
        {
            // Arrange
            TestAggregate testAggregate = new TestAggregate(Guid.NewGuid().ToEventStreamIdFormattedString());

            // Act
            testAggregate.BusinessLogicThatResultsInEventA(string.Empty);

            // Assert
            Assert.NotNull(testAggregate.LastTestMessageA);
            Assert.Equal(1, testAggregate.UncommittedEvents.Count);
            Assert.Equal(typeof(TestMessageA), testAggregate.UncommittedEvents[0].Body.GetType());
        }
Esempio n. 4
0
        public void Given_An_Event_And_Matching_UpdateState_Method_When_The_Event_Is_Raised_Then_The_Correct_UpdateState_Method_Is_Called_On_The_Aggregate()
        {
            // Arrange
            string        eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            // Act
            testAggregate.BusinessLogicThatResultsInEventA("myValue");

            // Assert
            Assert.NotNull(testAggregate.LastTestMessageA);
            Assert.Equal("myValue", testAggregate.LastTestMessageA.Stuff);
        }
Esempio n. 5
0
        public async Task Given_An_Aggregate_With_No_Stream_And_No_Snapshots_When_Loading_From_Latest_Snapshot_Then_The_Aggregate_Should_Be_Empty()
        {
            // Arrange
            IDomainRepository domainRepository    = GetDomainRepository();
            string            eventStreamId       = Guid.NewGuid().ToEventStreamIdFormattedString();
            TestAggregate     testAggregateToLoad = new TestAggregate(eventStreamId);

            // Act
            await domainRepository.LoadFromLatestSnapshotIfExistsAsync(testAggregateToLoad).ConfigureAwait(false);

            // Assert
            Assert.Equal(0, testAggregateToLoad.EventStreamRevision);
        }
Esempio n. 6
0
        public async Task Given_An_Invalid_Event_Stream_Id_When_Load_Is_Called_Then_An_Error_Is_Raised()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string        eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            // Act
            // Assert
            await AssertExtensions.ThrowsAsync <EventStreamNotFoundException>(
                async() => await eventStoreDomainRepository.LoadAsync(testAggregate).ConfigureAwait(false)).ConfigureAwait(false);
        }
Esempio n. 7
0
        public async Task Given_An_Invalid_Event_Stream_Id_When_The_Exists_Check_Is_Called_Using_The_Aggregate_Reference_Then_The_Aggregate_Should_Report_As_Not_Existing()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string        eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            // Act
            bool existsByReference = await eventStoreDomainRepository.ExistsAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.False(existsByReference);
        }
Esempio n. 8
0
        public async Task Given_An_Invalid_Event_Stream_Id_When_LoadIfExists_Is_Called_Then_False_Is_Returned()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string        eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            // Act
            bool aggregateLoaded = await eventStoreDomainRepository.LoadIfExistsAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.False(aggregateLoaded);
        }
Esempio n. 9
0
        public void Given_An_Aggregate_When_An_Event_Is_Applied_Then_The_Number_Of_Events_Since_Last_Snapshot_Count_Increases_By_One()
        {
            // Arrange
            TestAggregate testAggregate = new TestAggregate(Guid.NewGuid().ToEventStreamIdFormattedString());

            // Act
            long countBefore = testAggregate.NumberOfEventsSinceLastSnapshot;

            testAggregate.BusinessLogicThatResultsInEventA("value");

            // Assert
            Assert.Equal(0, countBefore);
            Assert.Equal(1, testAggregate.NumberOfEventsSinceLastSnapshot);
        }
Esempio n. 10
0
        public void Given_Multiple_Raised_Events_When_The_Aggregate_Is_Not_Saved_Then_StreamRevision_Should_Be_Zero()
        {
            // Arrange
            string         eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            ITestAggregate aggregate     = new TestAggregate(eventStreamId);

            // Act
            for (int i = 0; i < 10; i++)
            {
                aggregate.BusinessLogicThatResultsInEventA(string.Empty);

                // Assert
                Assert.Equal(0, aggregate.EventStreamRevision);
            }
        }
Esempio n. 11
0
        public async Task Given_An_Aggregate_Which_Has_Not_Been_Saved_When_A_Snapshot_Is_Taken_Then_An_Error_Is_Raised()
        {
            // Arrange
            IEventStoreProvider        eventStoreProvider         = GetEventStoreProvider();
            EventStoreDomainRepository eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);

            // Assert
            await AssertExtensions.ThrowsAsync <EventStreamNotFoundException>(
                async() => await eventStoreDomainRepository.SaveSnapshotAsync(testAggregateToSave).ConfigureAwait(false)).ConfigureAwait(false);
        }
Esempio n. 12
0
        public async Task Given_An_Uncommitted_Event_For_The_Aggregate_When_Save_Is_Called_Then_The_Event_Is_Added_To_The_Event_Store_And_The_EventStreamRevision_And_UncommittedEvents_Are_Updated_On_The_Aggregate()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.Equal(0, testAggregate.UncommittedEvents.Count);
            Assert.Equal(1, testAggregate.EventStreamRevision);
        }
Esempio n. 13
0
        public async Task Given_An_Uncommitted_Event_For_The_Aggregate_When_Save_Is_Called_Then_The_Event_Is_Added_To_The_Event_Store()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            IEnumerable <EventStoreMessage> eventStoreMessages = await eventStoreProvider.ReadEventsAsync(eventStreamId).ConfigureAwait(false);

            // Assert
            Assert.Single(eventStoreMessages);
        }
Esempio n. 14
0
        public async Task Given_A_Valid_Event_Stream_Id_When_The_Exists_Check_Is_Called_Using_The_Aggregate_Reference_Then_The_Aggregate_Should_Report_As_Not_Existing()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregateToSave).ConfigureAwait(false);

            bool existsByReference = await eventStoreDomainRepository.ExistsAsync(testAggregateToSave).ConfigureAwait(false);

            // Assert
            Assert.True(existsByReference);
        }
Esempio n. 15
0
        public async Task Given_An_Aggregate_With_No_Snapshots_When_Loading_From_Latest_Snapshot_Then_The_Aggregate_Should_Be_Loaded_From_The_Stream()
        {
            // Arrange
            IDomainRepository domainRepository = GetDomainRepository();
            string            eventStreamId    = Guid.NewGuid().ToEventStreamIdFormattedString();
            Guid eventId = Guid.NewGuid();

            // Act
            TestAggregate testAggregateToSaveAndSnapshot = new TestAggregate(eventStreamId);

            testAggregateToSaveAndSnapshot.BusinessLogicThatResultsInEventA("some value 1", eventId);
            await domainRepository.SaveAsync(testAggregateToSaveAndSnapshot).ConfigureAwait(false);

            TestAggregate testAggregateToLoad = new TestAggregate(eventStreamId);
            await domainRepository.LoadFromLatestSnapshotIfExistsAsync(testAggregateToLoad).ConfigureAwait(false);

            // Assert
            Assert.Equal(1, testAggregateToLoad.EventStreamRevision);
        }
Esempio n. 16
0
        public async Task Given_An_Empty_Stream_When_An_Event_Is_Written_To_The_Stream_Through_SaveWithoutConcurrencyCheck_Then_Aggregate_Reflects_The_Correct_Stream_Revision()
        {
            // Arrange
            IEventStoreProvider        eventStoreProvider         = GetEventStoreProvider();
            EventStoreDomainRepository eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            Guid   eventIdThatWeAreGoingToReUseToCauseAnIdempotentAppend = Guid.NewGuid();

            // Act
            TestAggregate testAggregateA = new TestAggregate(eventStreamId);

            testAggregateA.BusinessLogicThatResultsInEventA("some value 1", eventIdThatWeAreGoingToReUseToCauseAnIdempotentAppend);
            await eventStoreDomainRepository.SaveWithoutConcurrencyCheckAsync(testAggregateA).ConfigureAwait(false);

            long eventStreamRevisionAfterFirstEvent = testAggregateA.EventStreamRevision;

            // Assert
            Assert.Equal(1, eventStreamRevisionAfterFirstEvent);
        }
Esempio n. 17
0
        public void Given_A_Valid_Snapshot_When_The_Snapshot_Is_Loaded_By_The_Aggregate_Then_The_Aggregate_State_Is_As_Expected()
        {
            // Arrange
            TestAggregate         testAggregate         = new TestAggregate(Guid.NewGuid().ToEventStreamIdFormattedString());
            const string          value                 = "the value";
            const int             streamRevision        = 4;
            TestAggregateSnapshot testAggregateSnapshot = new TestAggregateSnapshot(new TestMessageA {
                Stuff = value
            });
            EventStoreSnapshot eventStoreSnapshot = new EventStoreSnapshot(Guid.NewGuid(), streamRevision, testAggregateSnapshot);

            // Act
            testAggregate.Load(eventStoreSnapshot);

            // Assert
            Assert.Equal(streamRevision, testAggregate.EventStreamRevision);
            Assert.Equal(0, testAggregate.UncommittedEvents.Count);
            Assert.Equal(value, testAggregate.LastTestMessageA.Stuff);
        }
        protected async Task Given_An_Aggregate_Throughout_The_Lifecycle_When_The_Number_Of_Events_Since_Last_Snapshot_Checked_Then_It_Reports_The_Correct_Value(IDomainRepository domainRepository, Func <TestAggregate, Task> loadAggregateAsync)
        {
            // Arrange
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA("value1");
            long numberOfEventsSinceLastSnapshotAfterApply = testAggregate.NumberOfEventsSinceLastSnapshot;
            await domainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            long          numberOfEventsSinceLastSnapshotAfterSave = testAggregate.NumberOfEventsSinceLastSnapshot;
            TestAggregate testAggregateLoadedFromStream            = new TestAggregate(eventStreamId);
            await domainRepository.LoadAsync(testAggregateLoadedFromStream).ConfigureAwait(false);

            long numberOfEventsSinceLastSnapshotAfterLoadFromStream = testAggregateLoadedFromStream.NumberOfEventsSinceLastSnapshot;
            await domainRepository.SaveSnapshotAsync(testAggregateLoadedFromStream).ConfigureAwait(false);

            long          numberOfEventsSinceLastSnapshotAfterSnapshot = testAggregateLoadedFromStream.NumberOfEventsSinceLastSnapshot;
            TestAggregate testAggregateLoadedFromSnapshot = new TestAggregate(eventStreamId);
            await loadAggregateAsync.Invoke(testAggregateLoadedFromSnapshot).ConfigureAwait(false);

            long numberOfEventsSinceLastSnapshotAfterLoadFromSnapshot = testAggregateLoadedFromSnapshot.NumberOfEventsSinceLastSnapshot;

            testAggregateLoadedFromSnapshot.BusinessLogicThatResultsInEventA("value2");
            await domainRepository.SaveAsync(testAggregateLoadedFromSnapshot).ConfigureAwait(false);

            TestAggregate testAggregateLoadedFromSnapshotAndSubsequentStreamEvents = new TestAggregate(eventStreamId);
            await loadAggregateAsync.Invoke(testAggregateLoadedFromSnapshotAndSubsequentStreamEvents).ConfigureAwait(false);

            long numberOfEventsSinceLastSnapshotAfterLoadFromSnapshotAndSubsequentStreamEvents = testAggregateLoadedFromSnapshotAndSubsequentStreamEvents.NumberOfEventsSinceLastSnapshot;

            // Assert
            Assert.Equal(1, numberOfEventsSinceLastSnapshotAfterApply);
            Assert.Equal(1, numberOfEventsSinceLastSnapshotAfterSave);
            Assert.Equal(1, numberOfEventsSinceLastSnapshotAfterLoadFromStream);
            Assert.Equal(0, numberOfEventsSinceLastSnapshotAfterSnapshot);
            Assert.Equal(0, numberOfEventsSinceLastSnapshotAfterLoadFromSnapshot);
            Assert.Equal(1, numberOfEventsSinceLastSnapshotAfterLoadFromSnapshotAndSubsequentStreamEvents);
        }
Esempio n. 19
0
        public async Task Given_A_Valid_Stream_Of_Events_When_The_Aggregate_Is_Loaded_Then_It_Does_So_Successfully()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            EventStoreMessage eventStoreMessage = new EventStoreMessage(Guid.NewGuid(), new TestMessageA());

            eventStoreProvider.AppendEvents(eventStreamId, eventStoreMessage);
            await eventStoreProvider.CommitEventsAsync(eventStreamId, ExpectedStreamRevision.New).ConfigureAwait(false);

            TestAggregate testAggregate = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.Equal(eventStreamId, testAggregate.EventStreamId);
            Assert.Equal(0, testAggregate.UncommittedEvents.Count);
            Assert.Equal(1, testAggregate.EventStreamRevision);
        }
Esempio n. 20
0
        public async Task Given_Three_Events_Stored_In_The_Event_Stream_In_A_Single_Operation_When_The_Aggregate_Is_Loaded_Then_The_Aggregate_Revision_Is_Three()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregateToSave).ConfigureAwait(false);

            TestAggregate testAggregateToLoad = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(testAggregateToLoad).ConfigureAwait(false);

            // Assert
            Assert.Equal(3, testAggregateToLoad.EventStreamRevision);
        }
Esempio n. 21
0
        public async Task Given_An_Aggregate_With_Uncommitted_Events_When_The_Aggregate_Is_Saved_Then_Afterwards_The_Aggregate_Can_Be_Loaded_Successfully()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregateToSave).ConfigureAwait(false);

            TestAggregate loadedTestAggregate = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(loadedTestAggregate).ConfigureAwait(false);

            // Assert
            Assert.Equal(eventStreamId, loadedTestAggregate.EventStreamId);
            Assert.Equal(0, loadedTestAggregate.UncommittedEvents.Count);
            Assert.Equal(1, loadedTestAggregate.EventStreamRevision);
        }
        protected async Task Given_An_Aggregate_With_Two_Events_That_Have_The_Same_Id_When_Saving_A_Snapshot_Then_The_Revision_Should_Be_One(IDomainRepository domainRepository, Func <TestAggregate, Task> loadAggregateAsync)
        {
            // Arrange
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            Guid   eventId       = Guid.NewGuid();

            // Act
            TestAggregate testAggregateToSaveAndSnapshot = new TestAggregate(eventStreamId);

            testAggregateToSaveAndSnapshot.BusinessLogicThatResultsInEventA("some value 1", eventId);
            testAggregateToSaveAndSnapshot.BusinessLogicThatResultsInEventA("some value 2", eventId);
            await domainRepository.SaveAsync(testAggregateToSaveAndSnapshot).ConfigureAwait(false);

            await domainRepository.SaveSnapshotAsync(testAggregateToSaveAndSnapshot).ConfigureAwait(false);

            TestAggregate testAggregateToLoad = new TestAggregate(eventStreamId);
            await loadAggregateAsync.Invoke(testAggregateToLoad).ConfigureAwait(false);

            // Assert
            Assert.Equal(1, testAggregateToLoad.EventStreamRevision);
        }
Esempio n. 23
0
        public async Task Given_An_Aggregate_Containing_Two_Events_With_The_Same_Id_When_The_Aggregate_Is_Saved_Then_Only_One_Event_Should_Written_To_The_Stream_And_A_Subsequent_Save_Of_The_Aggregate_Should_Succeed_Because_The_Stream_Revision_Of_The_Aggregate_Is_Kept_In_Sync()
        {
            // Arrange
            IEventStoreProvider        eventStoreProvider         = GetEventStoreProvider();
            EventStoreDomainRepository eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            Guid   eventIdA      = Guid.NewGuid();
            Guid   eventIdB      = Guid.NewGuid();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA("some value 1", eventIdA);
            testAggregate.BusinessLogicThatResultsInEventA("some value 2", eventIdA);
            await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            testAggregate.BusinessLogicThatResultsInEventA("some value 3", eventIdB);
            await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.Equal(2, testAggregate.EventStreamRevision);
        }
        protected async Task Given_An_Aggregate_With_Snapshot_And_One_Event_In_The_Stream_Since_The_Snapshot_Then_The_Number_Of_Events_Since_Last_Snapshot_Is_One(IDomainRepository domainRepository, Func <TestAggregate, Task> loadAggregateAsync)
        {
            // Arrange
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA("value1");
            await domainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            await domainRepository.SaveSnapshotAsync(testAggregate).ConfigureAwait(false);

            testAggregate.BusinessLogicThatResultsInEventA("value2");
            await domainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            TestAggregate testAggregateLoadedFromSnapshot = new TestAggregate(eventStreamId);
            await loadAggregateAsync.Invoke(testAggregateLoadedFromSnapshot).ConfigureAwait(false);

            // Assert
            Assert.Equal(1, testAggregateLoadedFromSnapshot.NumberOfEventsSinceLastSnapshot);
        }
Esempio n. 25
0
        public async Task Given_A_Separate_Update_To_The_Event_Stream_When_SaveWithoutConcurrencyCheck_Is_Used_Then_No_Error_Is_Raised()
        {
            // Arrange
            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            // Act
            // Create an aggregate with some events and save it.
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(testAggregateToSave).ConfigureAwait(false);

            // Load a new instance of the aggregate and add another event.
            TestAggregate loadedTestAggregateA = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(loadedTestAggregateA).ConfigureAwait(false);

            loadedTestAggregateA.BusinessLogicThatResultsInEventA(string.Empty);

            // Load another instance of the aggregate, add another event and save it.
            TestAggregate loadedTestAggregateB = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(loadedTestAggregateB).ConfigureAwait(false);

            loadedTestAggregateB.BusinessLogicThatResultsInEventA(string.Empty);
            await eventStoreDomainRepository.SaveAsync(loadedTestAggregateB).ConfigureAwait(false);

            // Save the first aggregate.
            await eventStoreDomainRepository.SaveWithoutConcurrencyCheckAsync(loadedTestAggregateA).ConfigureAwait(false);

            // Reload the aggregate so we can check it.
            TestAggregate testAggregate = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(testAggregate).ConfigureAwait(false);

            // Assert.
            // Check that all 3 events have been applied to the aggregate.
            Assert.Equal(3, testAggregate.EventStreamRevision);
        }
Esempio n. 26
0
        public async Task Given_Events_Stored_For_The_Event_Stream_When_LoadIfExists_Is_Called_Then_True_Is_Returned_And_The_Aggregate_Can_Be_Loaded_Successfully()
        {
            // Arrange
            string eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();

            IEventStoreProvider eventStoreProvider         = GetEventStoreProvider();
            IDomainRepository   eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);

            eventStoreProvider.AppendEvents(eventStreamId, new EventStoreMessage(Guid.NewGuid(), new TestMessageA()));
            await eventStoreProvider.CommitEventsAsync(eventStreamId, ExpectedStreamRevision.New).ConfigureAwait(false);

            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            // Act
            bool aggregateLoaded = await eventStoreDomainRepository.LoadIfExistsAsync(testAggregate).ConfigureAwait(false);

            // Assert
            Assert.True(aggregateLoaded);
            Assert.Equal(eventStreamId, testAggregate.EventStreamId);
            Assert.Equal(0, testAggregate.UncommittedEvents.Count);
            Assert.Equal(1, testAggregate.EventStreamRevision);
            Assert.NotNull(testAggregate.LastTestMessageA);
        }
        protected async Task Given_An_Aggregate_Which_Has_Been_Saved_When_A_Snapshot_Is_Taken_Then_The_Aggregate_Can_Be_Loaded_From_The_Latest_Snapshot(IDomainRepository domainRepository, Func <TestAggregate, Task> loadAggregateAsync)
        {
            // Arrange
            string       eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            const string testValue     = "some value";

            // Act
            TestAggregate testAggregateToSave = new TestAggregate(eventStreamId);

            testAggregateToSave.BusinessLogicThatResultsInEventA(testValue);
            await domainRepository.SaveAsync(testAggregateToSave).ConfigureAwait(false);

            await domainRepository.SaveSnapshotAsync(testAggregateToSave).ConfigureAwait(false);

            TestAggregate testAggregateToLoad = new TestAggregate(eventStreamId);
            await loadAggregateAsync.Invoke(testAggregateToLoad).ConfigureAwait(false);

            // Assert
            Assert.Equal(testAggregateToSave.EventStreamId, testAggregateToLoad.EventStreamId);
            Assert.Equal(testAggregateToSave.EventStreamRevision, testAggregateToLoad.EventStreamRevision);
            Assert.Equal(testValue, testAggregateToLoad.LastTestMessageA.Stuff);
            Assert.Equal(0, testAggregateToLoad.UncommittedEvents.Count);
        }
Esempio n. 28
0
        public async Task Given_An_Aggregate_Containing_Two_Events_With_The_Same_Id_When_The_Aggregate_Is_Saved_Then_Only_The_First_Event_Should_Written_To_The_Stream()
        {
            // Arrange
            IEventStoreProvider        eventStoreProvider         = GetEventStoreProvider();
            EventStoreDomainRepository eventStoreDomainRepository = new EventStoreDomainRepository(eventStoreProvider);
            string       eventStreamId = Guid.NewGuid().ToEventStreamIdFormattedString();
            const string expectedValue = "the value we should end up with";
            Guid         eventId       = Guid.NewGuid();

            // Act
            TestAggregate testAggregate = new TestAggregate(eventStreamId);

            testAggregate.BusinessLogicThatResultsInEventA(expectedValue, eventId);
            testAggregate.BusinessLogicThatResultsInEventA("some other value", eventId);
            await eventStoreDomainRepository.SaveAsync(testAggregate).ConfigureAwait(false);

            // Assert
            TestAggregate testAggregateThatIsLoaded = new TestAggregate(eventStreamId);
            await eventStoreDomainRepository.LoadAsync(testAggregateThatIsLoaded).ConfigureAwait(false);

            Assert.Equal(1, testAggregateThatIsLoaded.EventStreamRevision);
            Assert.Equal(0, testAggregateThatIsLoaded.UncommittedEvents.Count);
            Assert.Equal(expectedValue, testAggregateThatIsLoaded.LastTestMessageA.Stuff);
        }