Esempio n. 1
0
        public async Task StoreAsync_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);

            await repo.StoreAsync(person);

            Func <Task> act = async() => await repo.StoreAsync(person2);

            await act.Should().ThrowAsync <EventStoreConcurrencyException>();
        }
Esempio n. 2
0
        public async Task StoreAsync_ExistingEntity_VersionIncremented()
        {
            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);

            await repo.StoreAsync(person);

            _dbContext.Persons.First().EntityVersion.Should().Be(3);
        }
        public async Task StoreAsync_ExistingEntity_NewEventsSaved()
        {
            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 = await repo.GetAsync(p => p.Id == 12);

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

            await repo.StoreAsync(person);

            await _dbContext.SaveChangesAsync();

            _dbContext.Persons.Count().Should().Be(1);
            _dbContext.Persons.First().Events.Count.Should().Be(3);
        }
Esempio n. 4
0
        public async Task StoreAsync_EntityWithSomeEvents_VersionCalculated()
        {
            var repo   = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));
            var person = new Person();

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

            await repo.StoreAsync(person);

            await _dbContext.SaveChangesAsync();

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