Example #1
0
        public async Task SaveAggregateWithSerializedEvent()
        {
            Bank bank = new Bank(43);

            bank.OpenAccount(new OpenAccount("Evert 't Reve"));
            var target = new BankRepository(_options);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var onlyEvent = rawContext.BankEvents.Single();

                Assert.AreEqual(1, onlyEvent.Version);
                Assert.AreEqual("DDD.Core.Application.Test.AccountOpened", onlyEvent.EventType);
                Assert.AreEqual("{\r\n  \"AccountNumber\": 1,\r\n  \"Owner\": \"Evert 't Reve\"\r\n}",
                                onlyEvent.EventData);
            }
        }
Example #2
0
        public async Task SaveAggregate_AlreadyInStoreWithEvents()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 5,\r\n  \"Owner\": \"Karina van Irak\"\r\n}",
                    },
                    new Raw.BankEvent
                    {
                        BankId    = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{\r\n  \"AccountNumber\": 6,\r\n  \"Owner\": \"Evert 't Reve\"\r\n}",
                    },
                };
                var bankje = new Raw.Bank {
                    Id = 7, Version = 2, Events = events
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var  target = new BankRepository(_options);
            Bank bank   = await target.FindAsync(7);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).FirstOrDefault();

                Assert.IsNotNull(bankFromDb);
                Assert.AreEqual(7, bankFromDb.Id);
                Assert.AreEqual(2, bankFromDb.Version);
                Assert.AreEqual(2, bankFromDb.Events.Count());
            }
        }
        public async Task LoadAggregateRootWithOnyEventsFromOtherAggregates()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var events = new List <Raw.BankEvent> {
                    new Raw.BankEvent
                    {
                        Id        = 1, BankId = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{ 'AccountNumber':5, 'Owner':'Karina van Irak' }",
                    },
                    new Raw.BankEvent
                    {
                        Id        = 2, BankId = 7, Version = 2,
                        EventType = "DDD.Core.Application.Test.AccountOpened",
                        EventData = "{ 'AccountNumber':6, 'Owner':'Evert \\'t Reve' }",
                    },
                };
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 7, Version = 12, Events = events
                });
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 5, Version = 12, Events = new List <Raw.BankEvent>()
                });
                rawContext.Banks.Add(new Raw.Bank {
                    Id = 3, Version = 12, Events = null
                });
                rawContext.SaveChanges();
            }

            var target = new BankRepository(_options);

            Bank bank = await target.FindAsync(5);

            Assert.AreEqual(5, bank.Id);
            Assert.AreEqual(0, bank.Version);
            Assert.AreEqual(0, bank.HandleAccountOpenedCallCount);
        }
        public async Task LoadAggregateRootWithZeroEvents()
        {
            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                rawContext.Database.EnsureCreated();

                var bankje = new Raw.Bank {
                    Id = 7, Version = 12,
                };
                rawContext.Banks.Add(bankje);
                rawContext.SaveChanges();
            }

            var target = new BankRepository(_options);

            Bank bank = await target.FindAsync(7);

            Assert.AreEqual(7, bank.Id);
            Assert.AreEqual(0, bank.Version);
            // the 'Version' should reflect the number of events that have been handled, which is 0, in this test.
            // this should be equal the the 'Version' in the database, but in this test the latter was manually set to 12.
        }
Example #5
0
        public async Task SaveAggregateWithTwoEvents()
        {
            Bank bank = new Bank(43);

            bank.OpenAccount(new OpenAccount("Karina van Irak"));
            bank.OpenAccount(new OpenAccount("Evert 't Reve"));
            Assert.AreEqual(2, bank.Events.Count());
            var target = new BankRepository(_options);

            await target.SaveAsync(bank);

            using (var rawContext = new Raw.RawContext(_rawOptions))
            {
                var bankFromDb = rawContext.Banks.Include(b => b.Events).Single(b => b.Id == 43);

                Assert.AreEqual(2, bankFromDb.Version);
                Assert.AreEqual(2, bankFromDb.Events.Count());
                Assert.IsTrue(bankFromDb.Events.Any(evt =>
                                                    evt.EventData.Contains("Karina van Irak")));
                Assert.IsTrue(bankFromDb.Events.Any(evt =>
                                                    evt.EventData.Contains("Evert 't Reve")));
            }
        }