public async Task SaveChangesAsync(IEnumerable <FulfillmentCenter> fulfillmentCenters) { if (fulfillmentCenters == null) { throw new ArgumentNullException(nameof(fulfillmentCenters)); } var pkMap = new PrimaryKeyResolvingMap(); var changedEntries = new List <GenericChangedEntry <FulfillmentCenter> >(); using (var repository = _repositoryFactory()) { var existEntities = await repository.GetFulfillmentCentersAsync(fulfillmentCenters.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray()); foreach (var changedCenter in fulfillmentCenters) { var existEntity = existEntities.FirstOrDefault(x => x.Id == changedCenter.Id); var modifiedEntity = AbstractTypeFactory <FulfillmentCenterEntity> .TryCreateInstance().FromModel(changedCenter, pkMap); if (existEntity != null) { changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(changedCenter, existEntity.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()), EntryState.Modified)); modifiedEntity.Patch(existEntity); } else { repository.Add(modifiedEntity); changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(changedCenter, EntryState.Added)); } } await _eventPublisher.Publish(new FulfillmentCenterChangingEvent(changedEntries)); await repository.UnitOfWork.CommitAsync(); pkMap.ResolvePrimaryKeys(); await _eventPublisher.Publish(new FulfillmentCenterChangedEvent(changedEntries)); FulfillmentCenterCacheRegion.ExpireRegion(); } }
public async Task DeleteAsync(IEnumerable <string> ids) { using (var repository = _repositoryFactory()) { var changedEntries = new List <GenericChangedEntry <FulfillmentCenter> >(); var dbCenters = await repository.GetFulfillmentCentersAsync(ids); foreach (var dbCenter in dbCenters) { repository.Remove(dbCenter); changedEntries.Add(new GenericChangedEntry <FulfillmentCenter>(dbCenter.ToModel(AbstractTypeFactory <FulfillmentCenter> .TryCreateInstance()), EntryState.Deleted)); } await _eventPublisher.Publish(new FulfillmentCenterChangingEvent(changedEntries)); await repository.UnitOfWork.CommitAsync(); await _eventPublisher.Publish(new FulfillmentCenterChangedEvent(changedEntries)); FulfillmentCenterCacheRegion.ExpireRegion(); } }