public virtual void Delete(string[] ids) { var orders = GetByIds(ids, CustomerOrderResponseGroup.Full.ToString()); using (var repository = RepositoryFactory()) { foreach (var order in orders) { OrderChangingPublisher.Publish(new OrderChangeEvent(EntryState.Deleted, order, order)); } repository.RemoveOrdersByIds(ids); foreach (var order in orders) { DynamicPropertyService.DeleteDynamicPropertyValues(order); } repository.UnitOfWork.Commit(); foreach (var order in orders) { OrderChangedPublisher.Publish(new OrderChangedEvent(EntryState.Deleted, order, order)); } } }
public virtual void SaveChanges(CustomerOrder[] orders) { var pkMap = new PrimaryKeyResolvingMap(); var changedEvents = new List <OrderChangedEvent>(); using (var repository = RepositoryFactory()) using (var changeTracker = GetChangeTracker(repository)) { var dataExistOrders = repository.GetCustomerOrdersByIds(orders.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray(), CustomerOrderResponseGroup.Full); foreach (var order in orders) { EnsureThatAllOperationsHaveNumber(order); var originalEntity = dataExistOrders.FirstOrDefault(x => x.Id == order.Id); var originalOrder = originalEntity != null ? (CustomerOrder)originalEntity.ToModel(AbstractTypeFactory <CustomerOrder> .TryCreateInstance()) : order; //Calculate order totals before event raising TotalsCalculator.CalculateTotals(order); var changingEvent = new OrderChangeEvent(originalEntity == null ? EntryState.Added : EntryState.Modified, originalOrder, order); OrderChangingPublisher.Publish(changingEvent); changedEvents.Add(new OrderChangedEvent(changingEvent.ChangeState, changingEvent.OrigOrder, changingEvent.ModifiedOrder)); //Calculate order totals after event raising but before save TotalsCalculator.CalculateTotals(order); var modifiedEntity = AbstractTypeFactory <CustomerOrderEntity> .TryCreateInstance() .FromModel(order, pkMap) as CustomerOrderEntity; if (originalEntity != null) { changeTracker.Attach(originalEntity); modifiedEntity?.Patch(originalEntity); } else { repository.Add(modifiedEntity); } } CommitChanges(repository); pkMap.ResolvePrimaryKeys(); } //Save dynamic properties foreach (var order in orders) { DynamicPropertyService.SaveDynamicPropertyValues(order); } //Raise changed events foreach (var changedEvent in changedEvents) { OrderChangedPublisher.Publish(changedEvent); } }