Beispiel #1
0
        public void Store_ConcurrentlyModifyExistingEntity_ExceptionThrown()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12, EntityVersion = 1
            }).Entity;

            eventStore.Events.Add(
                new PersonEvent
            {
                SerializedEvent = "{\"NewName\": \"Silverback\"}",
                ClrType         =
                    "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests"
            });
            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

            var person  = repo.Find(p => p.Id == 12);
            var person2 = repo.Find(p => p.Id == 12);

            person !.ChangeName("Sergio");
            person.ChangeAge(35);
            person2 !.ChangeName("Sergio");
            person2.ChangeAge(35);

            repo.Store(person);
            Action act = () => repo.Store(person2);

            act.Should().Throw <EventStoreConcurrencyException>();
        }
Beispiel #2
0
        public void Store_ExistingEntity_NewEventsSaved()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12, EntityVersion = 1
            }).Entity;

            eventStore.Events.Add(
                new PersonEvent
            {
                SerializedEvent = "{\"NewName\": \"Silverback\"}",
                ClrType         =
                    "Silverback.Tests.EventSourcing.TestTypes.EntityEvents.NameChangedEvent, Silverback.EventSourcing.Tests"
            });
            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

            var person = repo.Find(p => p.Id == 12);

            person !.ChangeName("Sergio");
            person.ChangeAge(35);

            repo.Store(person);
            _dbContext.SaveChanges();

            _dbContext.Persons.Should().HaveCount(1);
            _dbContext.Persons.First().Events.Should().HaveCount(3);
        }
        public void Store_ExistingEntity_VersionIncremented()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12, EntityVersion = 1
            }).Entity;

            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}"
            });
            _dbContext.SaveChanges();

            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

            var person = repo.Get(p => p.Id == 12);

            person.ChangeName("Sergio");
            person.ChangeAge(35);

            repo.Store(person);

            _dbContext.Persons.First().EntityVersion.Should().Be(3);
        }
Beispiel #4
0
        public void Store_EntityWithSomeEvents_VersionCalculated()
        {
            var repo   = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));
            var person = new Person();

            person.ChangeName("Sergio");
            person.ChangeAge(35);

            repo.Store(person);
            _dbContext.SaveChanges();

            _dbContext.Persons.First().EntityVersion.Should().Be(2);
        }