public virtual void SaveChanges(ShoppingCart[] carts) { var pkMap = new PrimaryKeyResolvingMap(); var changedEvents = new List <CartChangedEvent>(); using (var repository = _repositoryFactory()) using (var changeTracker = GetChangeTracker(repository)) { var dataExistCarts = repository.GetShoppingCartsByIds(carts.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray()); foreach (var cart in carts) { //Calculate cart totals before save _totalsCalculator.CalculateTotals(cart); var originalEntity = dataExistCarts.FirstOrDefault(x => x.Id == cart.Id); var originalCart = originalEntity != null?originalEntity.ToModel(AbstractTypeFactory <ShoppingCart> .TryCreateInstance()) : cart; var changingEvent = new CartChangeEvent(originalEntity == null ? EntryState.Added : EntryState.Modified, originalCart, cart); _changingEventPublisher.Publish(changingEvent); changedEvents.Add(new CartChangedEvent(changingEvent.ChangeState, changingEvent.OrigCart, changingEvent.ModifiedCart)); var modifiedEntity = AbstractTypeFactory <ShoppingCartEntity> .TryCreateInstance().FromModel(cart, pkMap); if (originalEntity != null) { changeTracker.Attach(originalEntity); modifiedEntity.Patch(originalEntity); } else { repository.Add(modifiedEntity); } } CommitChanges(repository); pkMap.ResolvePrimaryKeys(); } //Save dynamic properties foreach (var cart in carts) { _dynamicPropertyService.SaveDynamicPropertyValues(cart); } //Raise changed events foreach (var changedEvent in changedEvents) { _changedEventPublisher.Publish(changedEvent); } }
public Task Handle(CartChangeEvent message) { return(Task.CompletedTask); }