示例#1
0
        public async Task UserCategoriesFromHistoryCanPopulateQueryDb()
        {
            await _aggregateRepository.Create(new AggregateRoot { EventVersion = 0, Version = 0, Id = 1 });

            UserCategoryCreatedEvent e2 = new UserCategoryCreatedEvent(1, "Category 1", DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 2, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryCreatedEvent>(e2) });

            UserCategoryNameChangedEvent e3 = new UserCategoryNameChangedEvent(1, "Category 1 Renamed Once", 0, DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 3, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryNameChangedEvent>(e3) });

            UserCategoryNameChangedEvent e4 = new UserCategoryNameChangedEvent(1, "Category 1 Renamed Twice", 1, DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 4, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryNameChangedEvent>(e4) });

            UserCategoryNameChangedEvent e5 = new UserCategoryNameChangedEvent(1, "Category 1 Renamed Three Times", 2, DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 5, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryNameChangedEvent>(e5) });

            UserCategoryNameChangedEvent e6 = new UserCategoryNameChangedEvent(1, "Category 1 Renamed Four Times", 3, DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 6, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryNameChangedEvent>(e6) });

            UserCategoryNameChangedEvent e7 = new UserCategoryNameChangedEvent(1, "Category 1 Renamed Five Times", 4, DateTime.UtcNow);
            await _eventRepository.Create(new Event { Id = 7, AggregateId = 1, Data = DatabaseEventStorage.Serialize <UserCategoryNameChangedEvent>(e7) });

            foreach (var @event in (await _eventRepository.FindByAsync(x => true)).ToList())
            {
                var e = DatabaseEventStorage.DeSerialize(@event.Data);

                if (e is UserCategoryNameChangedEvent)
                {
                    var handlers = _eventHandlerFactory.GetHandlers <UserCategoryNameChangedEvent>();
                    foreach (var eventHandler in handlers)
                    {
                        eventHandler.Handle(e);
                    }
                }

                if (e is UserCategoryCreatedEvent)
                {
                    var handlers = _eventHandlerFactory.GetHandlers <UserCategoryCreatedEvent>();
                    foreach (var eventHandler in handlers)
                    {
                        eventHandler.Handle(e);
                    }
                }
            }

            var cats = await _categoryRepo.FindByAsync(q => q.Id == 1).ConfigureAwait(false);

            var cat = cats.FirstOrDefault();

            Assert.AreEqual("Category 1 Renamed Five Times", cat.Name);
        }
示例#2
0
        public async Task UserCategoryCreateCommandStoresEventInEventStore()
        {
            //Arrange
            var catName = "Test Category 345";
            var command = new UserCategoryCreateCommand(catName);

            //Act
            await _commandBus.Send(command);

            var e = (await _eventRepository.FindByAsync(x => x.AggregateId == 1)).FirstOrDefault();

            //Assert
            Assert.IsNotNull(e);
            var desrializedEvent = (UserCategoryCreatedEvent)DatabaseEventStorage.DeSerialize <UserCategoryCreatedEvent>(e.Data);

            Assert.IsNotNull(desrializedEvent);
            Assert.AreEqual(catName, desrializedEvent.Name);
        }
示例#3
0
        public async Task UserCategoryAggregateRootCanBeConstructedFromMemento()
        {
            // Arrange
            // No version - it's new
            var e1 = new UserCategoryCreateCommand("Category Name");
            // Acting on Version 0
            var e2 = new UserCategoryNameChangeCommand(1, "Category Name Changed Once", 0);
            // Acting on Version 1
            var e3 = new UserCategoryNameChangeCommand(1, "Category Name Changed Twice", 1);
            // Acting on Version 2
            var e4 = new UserCategoryNameChangeCommand(1, "Category Name Changed Three Times", 2);
            // Acting on Version 3
            var e5 = new UserCategoryNameChangeCommand(1, "Category Name Changed Four Times", 3);

            // Act
            await _commandBus.Send <UserCategoryCreateCommand>(e1);

            await _commandBus.Send <UserCategoryNameChangeCommand>(e2);

            await _commandBus.Send <UserCategoryNameChangeCommand>(e3);

            await _commandBus.Send <UserCategoryNameChangeCommand>(e4);

            await _commandBus.Send <UserCategoryNameChangeCommand>(e5);


            // Assert
            var m = (await _mementoRepository.FindByAsync(x => x.Id == 1)).FirstOrDefault();

            Assert.IsNotNull(m);
            var          memento = DatabaseEventStorage.DeSerialize <UserCategoryMemento>(m.Data);
            UserCategory c       = new UserCategory();

            c.SetMemento(memento);
            Assert.AreEqual("Category Name Changed Three Times", c.Name);
        }
示例#4
0
        public override Event ToAnyEntity(Providers.EventStore.Entities.Event source)
        {
            var target = DatabaseEventStorage.DeSerialize <Event>(source.Data);

            return(target);
        }
示例#5
0
        public override BaseMemento ToAnyEntity(Providers.EventStore.Entities.BaseMemento source)
        {
            var target = DatabaseEventStorage.DeSerialize <BaseMemento>(source.Data);

            return(target);
        }