Esempio n. 1
0
        public void Aggregate_ShouldBeRebuildedFromSnapshot_WhenSnapshotDataAvailable()
        {
            var aggStore = new AggregateStore(new EventStore(new DefaultEventSerializer(), new EventDataStorage()), new DefaultAggregateSerializer(), new SnapshotDataStorage());

            var car = new Car();

            car.ChangeTyre(new Tyre()
            {
                Price = 10
            }, TyrePlacement.LeftBack);
            car.ChangeTyre(new Tyre()
            {
                Price = 11
            }, TyrePlacement.RightBack);
            car.ChangeTyre(new Tyre()
            {
                Price = 12
            }, TyrePlacement.RightBack);
            car.ChangeTyre(new Tyre()
            {
                Price = 13
            }, TyrePlacement.RightBack);
            car.ChangeTyre(new Tyre()
            {
                Price = 14
            }, TyrePlacement.RightBack);
            car.ChangeTyre(new Tyre()
            {
                Price = 15
            }, TyrePlacement.RightFront);

            aggStore.Save(car);

            var carFromSnapshot = aggStore.Get <Car>(car.Id);

            carFromSnapshot.ChangeTyre(new Tyre()
            {
                Price = 17
            }, TyrePlacement.RightBack);
            carFromSnapshot.ChangeTyre(new Tyre()
            {
                Price = 16
            }, TyrePlacement.RightFront);

            aggStore.Save(carFromSnapshot);

            var carFromSnapshot2 = aggStore.Get <Car>(car.Id);

            Assert.Equal(8, carFromSnapshot2.Version);
            Assert.Equal(16, carFromSnapshot2.RightFrontTyre.Price);
        }
Esempio n. 2
0
        public void When_merging()
        {
            var ms      = new MergeService();
            var mergeID = ms.Merge(_primary, _secondary);

            _primary.PushEvent(new MobilePhoneChangedEvent("0744 4444 444"));
            _store.Save("Users", _primary);

            _primary.ShouldSatisfyAllConditions(
                () => _primary.Name.ShouldBe("Andrew"),
                () => _primary.Phones[PhoneType.Mobile].ShouldBe("0744 4444 444"),
                () => _primary.Phones[PhoneType.Home].ShouldBe("01412 123 123")
                );
        }
Esempio n. 3
0
        public Tests()
        {
            _store = new AggregateStore <int>(new InMemoryEventStore());

            _primary = User.Create(1, "Andy");
            _primary.PushEvent(new MobilePhoneChangedEvent("0798 1234 123"));
            _primary.PushEvent(new HomePhoneChangedEvent("01412 123 123"));
            _primary.PushEvent(new DateOfBirthChangedEvent(new DateTime(1980, 7, 12)));

            _secondary = User.Create(2, "Andrew");
            _secondary.PushEvent(new MobilePhoneChangedEvent("0798 9876 987"));

            _store.Save("Users", _primary);
            _store.Save("Users", _secondary);
        }
        public async Task ShouldSuccesfullyPersistAndHydrateEventsFromAggregate()
        {
            // ARRANGE
            var testAggregate = new TestAggregate(Guid.NewGuid());

            //var eventRepository = new SqlServerEventRepository("Server=tcp:shuffle.database.windows.net,1433;Initial Catalog=Shuffle;Persist Security Info=False;User ID=kvinther;Password=k1617v_KV;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Application Name=Shuffle;");
            var eventRepository  = new SqlServerEventRepository("Server=.\\sqlexpress;Initial Catalog=VerdunEvents;Persist Security Info=False;Trusted_Connection=True;MultipleActiveResultSets=False;Connection Timeout=30;Application Name=Shuffle;");
            var aggregateFactory = new DefaultAggregateFactory();
            var repository       = new AggregateRepository(eventRepository, aggregateFactory);
            var listener         = new BasicEventListener();

            IAggregateCache cache = null;

            //cache = new RedisAggregateCache("shuffle.redis.cache.windows.net:6380,password=z0cYd5K7aNjtE9tl8x6nxYa2lK5TwrJcB1aHsZGCx5Q=,ssl=True,abortConnect=False");
            cache = new RedisAggregateCache("localhost");
            var store = new AggregateStore(repository, listener, aggregateCache: cache);

            // ACT
            var now = DateTime.Now;

            for (var i = 1; i <= 50; i++)
            {
                testAggregate.PublishTestEvent($"{i}");
                await store.Save(testAggregate);

                var hydratedTestAggregate = store.GetById <TestAggregate>(testAggregate.Id);
                // ASSERT
                Assert.AreEqual(testAggregate.Id, hydratedTestAggregate.Id);
                Assert.AreEqual(testAggregate.Version, hydratedTestAggregate.Version);
                Assert.AreEqual(testAggregate.State, hydratedTestAggregate.State);
            }


            Debug.WriteLine($"Total time: {(DateTime.Now - now):g}");
        }
        private void Execute(ChangeUser command)
        {
            var user = AggregateStore.GetAggregate <User>(command.Id);

            user.Change(command.Name, command.EMail, command.Password);
            AggregateStore.Save(user);
        }
        private void Execute(DeleteUser command)
        {
            var user = AggregateStore.GetAggregate <User>(command.Id);

            user.Delete();
            AggregateStore.Save(user);
        }
Esempio n. 7
0
        public CommandStatus Handle(CreateUserCommand message)
        {
            var user = new UserAggregate(_userService, message.Key, message.Name);

            _userStore.Save("Users", user);

            return(CommandStatus.Accepted);
        }
        public CommandStatus Handle(ChangeUsersNameCommand message)
        {
            var user = _userStore.Load("Users", message.UserID, UserAggregate.Blank);

            user.ChangeName(message.NewName);

            _userStore.Save("Users", user);

            return(CommandStatus.Accepted);
        }
            public void SaveSnapshotIfRequired()
            {
                var aggregate      = new FakeAggregate(GuidStrategy.NewGuid(), 9);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings());

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                snapshotStore.Verify(mock => mock.Save(It.Is <Snapshot>(s => s.StreamId == aggregate.Id && s.Version == 10)), Times.Once());
            }
            public void CaptureAggregateTypeIfFirstCommit()
            {
                var aggregate      = new FakeAggregate(GuidStrategy.NewGuid(), 0);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object);

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                eventStore.Verify(mock => mock.Save(It.Is <Commit>(c => c.Headers[Header.Aggregate] == typeof(FakeAggregate).GetFullNameWithAssembly())), Times.Once());
            }
            public void IncrementAggregateVersionIfSuccessful()
            {
                var version        = 11;
                var aggregate      = new FakeAggregate(GuidStrategy.NewGuid(), version);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object);

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                Assert.Equal(version + 1, aggregate.Version);
            }
            public void IgnoreDuplicateCommits()
            {
                var aggregate      = new FakeAggregate(GuidStrategy.NewGuid(), 8);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings());

                eventStore.Setup(mock => mock.Save(It.IsAny <Commit>())).Throws <DuplicateCommitException>();

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                eventStore.Verify(mock => mock.Save(It.IsAny <Commit>()), Times.Once);
            }
            public void CaptureAggregateTypeIfFirstCommit()
            {
                var aggregate = new FakeAggregate(GuidStrategy.NewGuid(), 0);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object);

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                eventStore.Verify(mock => mock.Save(It.Is<Commit>(c => c.Headers[Header.Aggregate] == typeof(FakeAggregate).GetFullNameWithAssembly())), Times.Once());
            }
            public void SaveSnapshotIfRequired()
            {
                var aggregate = new FakeAggregate(GuidStrategy.NewGuid(), 9);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings());

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                snapshotStore.Verify(mock => mock.Save(It.Is<Snapshot>(s => s.StreamId == aggregate.Id && s.Version == 10)), Times.Once());
            }
            public void IgnoreDuplicateCommits()
            {
                var aggregate = new FakeAggregate(GuidStrategy.NewGuid(), 8);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object, new AggregateStoreSettings());

                eventStore.Setup(mock => mock.Save(It.IsAny<Commit>())).Throws<DuplicateCommitException>();

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                eventStore.Verify(mock => mock.Save(It.IsAny<Commit>()), Times.Once);
            }
Esempio n. 16
0
        private Permission Clone(Permission permission)
        {
            var store = new AggregateStore<Guid>(new InMemoryEventStore());
            store.Save("test", permission);

            return store.Load("test", permission.ID, () => Permission.Blank());
        }
            public void IncrementAggregateVersionIfSuccessful()
            {
                var version = 11;
                var aggregate = new FakeAggregate(GuidStrategy.NewGuid(), version);
                var aggregateStore = new AggregateStore(aggregateUpdater.Object, snapshotStore.Object, eventStore.Object);

                using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty))
                    aggregateStore.Save(aggregate, context);

                Assert.Equal(version + 1, aggregate.Version);
            }