コード例 #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 = "{" +
                                  "\"$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);
            var person2 = repo.Get(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);

            act.Should().Throw <SilverbackConcurrencyException>();
        }
コード例 #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 = "{" +
                                  "\"$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.SaveChanges();

            _dbContext.Persons.Count().Should().Be(1);
            _dbContext.Persons.First().Events.Count.Should().Be(3);
        }
コード例 #3
0
        public void Remove_ExistingEntity_EntityDeleted()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).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 entity = repo.Get(p => p.Id == 12);

            entity.Should().NotBeNull();

            repo.Remove(entity);
            _dbContext.SaveChanges();

            _dbContext.Persons.Count().Should().Be(0);
            _dbContext.Persons.SelectMany(s => s.Events).Count().Should().Be(0);
        }
コード例 #4
0
        public void Get_ExistingId_EventsAppliedInRightOrder()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Silverback\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-05-05")
            });
            eventStore.Events.Add(new PersonEvent
            {
                SerializedEvent = "{" +
                                  "\"$type\": \"Silverback.Tests.EventSourcing.TestTypes.Person+NameChangedEvent, Silverback.EventSourcing.Tests\"," +
                                  "\"NewName\": \"Sergio\"" +
                                  "}",
                Timestamp = DateTime.Parse("2000-03-01")
            });

            _dbContext.SaveChanges();

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

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

            entity.Name.Should().Be("Silverback");
        }
コード例 #5
0
        public void Get_ExistingId_EventsApplied()
        {
            var eventStore = _dbContext.Persons.Add(new PersonEventStore {
                Id = 12
            }).Entity;

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

            _dbContext.SaveChanges();

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

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

            entity.Name.Should().Be("Silverback");
            entity.Age.Should().Be(35);
        }
コード例 #6
0
        public void Get_NonExistingId_NullReturned()
        {
            var repo = new PersonDbEventStoreRepository(new EfCoreDbContext <TestDbContext>(_dbContext));

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

            entity.Should().BeNull();
        }