Example #1
0
        public async Task UserCategoryUpdateCommandTriggersEventToUpdateQueryDbWithNewCategoryName()
        {
            //Arrange
            string newName = "NewCategoryNameForCategory2";

            await _categoryRepo.Create(new Category { Id = 1, Name = "Category1", Version = 0 });

            await _categoryRepo.Create(new Category { Id = 2, Name = "Category2", Version = 0 });

            await _categoryRepo.Create(new Category { Id = 3, Name = "Category3", Version = 0 });

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

            await _aggregateRepository.Create(new AggregateRoot { EventVersion = 0, Id = 1 });

            //Act
            await _commandBus.Send(new UserCategoryNameChangeCommand(1, newName, 0));

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

            var cat = cats.FirstOrDefault();

            //Assert
            Assert.AreEqual(newName, cat.Name);
            Assert.AreEqual(1, cat.Version);
        }
        public async Task Save(AggregateRoot aggregate)
        {
            var uncommittedChanges = aggregate.GetUncommittedChanges();
            var version            = aggregate.Version;

            foreach (var @event in uncommittedChanges)
            {
                version++;
                if (version > 2)
                {
                    if (version % 3 == 0)
                    {
                        var originator = (IOriginator)aggregate;
                        var memento    = originator.GetMemento();
                        memento.Version = version;
                        await SaveMemento(memento).ConfigureAwait(false);
                    }
                }

                @event.Version    = version;
                aggregate.Version = version;

                if (version == 0)
                {
                    var ag = await _aggregateRepository.Create(_aggregateMapper.ToProviderEntity(aggregate)).ConfigureAwait(false);

                    @event.AggregateId = ag.Id;
                }
                else
                {
                    var aggregateRoots = await _aggregateRepository.FindByAsync(x => x.Id == aggregate.Id).ConfigureAwait(false);

                    var aggregateRoot = aggregateRoots.FirstOrDefault();

                    if (aggregateRoot == null)
                    {
                        throw new NullReferenceException();
                    }

                    aggregateRoot.Version = version;
                    await _aggregateRepository.Update(aggregateRoot).ConfigureAwait(false);
                }

                await _eventRepository.Create(_eventMapper.ToProviderEntity(@event)).ConfigureAwait(false);
            }

            foreach (var @event in uncommittedChanges)
            {
                var desEvent = Converter.ChangeTo(@event, @event.GetType());
                await _eventBus.Publish(desEvent).ConfigureAwait(false);
            }
        }