public virtual async Task DeleteUsagesAsync(string[] ids) { using (var repository = _repositoryFactory()) { await repository.RemoveMarketingUsagesAsync(ids); await repository.UnitOfWork.CommitAsync(); } PromotionUsageCacheRegion.ExpireRegion(); }
public virtual async Task SaveUsagesAsync(PromotionUsage[] usages) { var pkMap = new PrimaryKeyResolvingMap(); var changedEntries = new List <GenericChangedEntry <PromotionUsage> >(); using (var repository = _repositoryFactory()) { var existUsageEntities = await repository.GetMarketingUsagesByIdsAsync(usages.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray()); foreach (var usage in usages) { var sourceEntity = AbstractTypeFactory <PromotionUsageEntity> .TryCreateInstance(); if (sourceEntity != null) { sourceEntity = sourceEntity.FromModel(usage, pkMap); var targetUsageEntity = existUsageEntities.FirstOrDefault(x => x.Id == usage.Id); if (targetUsageEntity != null) { changedEntries.Add(new GenericChangedEntry <PromotionUsage>(usage, sourceEntity.ToModel(AbstractTypeFactory <PromotionUsage> .TryCreateInstance()), EntryState.Modified)); sourceEntity.Patch(targetUsageEntity); } else { changedEntries.Add(new GenericChangedEntry <PromotionUsage>(usage, EntryState.Added)); repository.Add(sourceEntity); } } } await repository.UnitOfWork.CommitAsync(); pkMap.ResolvePrimaryKeys(); await _eventPublisher.Publish(new PromotionUsageChangedEvent(changedEntries)); } PromotionUsageCacheRegion.ExpireRegion(); }