public async Task <T> GetMemento <T>(int aggregateId) where T : BaseMemento { var mementos = await _mementoRepository.FindByAsync(m => m.Id == aggregateId).ConfigureAwait(false); var memento = mementos.LastOrDefault(); if (memento != null) { return((T)_mementoMapper.ToAnyEntity(memento)); } return(null); }
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); }