public async Task SaveChangesAsync(PropertyDictionaryItem[] dictItems) { if (dictItems == null) { throw new ArgumentNullException(nameof(dictItems)); } var pkMap = new PrimaryKeyResolvingMap(); using (var repository = _repositoryFactory()) { var dbExistEntities = await repository.GetPropertyDictionaryItemsByIdsAsync(dictItems.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray()); foreach (var dictItem in dictItems) { var originalEntity = dbExistEntities.FirstOrDefault(x => x.Id == dictItem.Id); var modifiedEntity = AbstractTypeFactory <PropertyDictionaryItemEntity> .TryCreateInstance().FromModel(dictItem, pkMap); if (originalEntity != null) { modifiedEntity.Patch(originalEntity); } else { repository.Add(modifiedEntity); } } await repository.UnitOfWork.CommitAsync(); pkMap.ResolvePrimaryKeys(); } DictionaryItemsCacheRegion.ExpireRegion(); }
public async Task DeleteAsync(string[] ids) { using (var repository = _repositoryFactory()) { var dbEntities = await repository.GetPropertyDictionaryItemsByIdsAsync(ids); foreach (var dbEntity in dbEntities) { repository.Remove(dbEntity); } await repository.UnitOfWork.CommitAsync(); } DictionaryItemsCacheRegion.ExpireRegion(); }
public Task <PropertyDictionaryItem[]> GetByIdsAsync(string[] ids) { var cacheKey = CacheKey.With(GetType(), "GetByIdsAsync", string.Join(",", ids)); return(_platformMemoryCache.GetOrCreateExclusive(cacheKey, async(cacheEntry) => { cacheEntry.AddExpirationToken(DictionaryItemsCacheRegion.CreateChangeToken()); PropertyDictionaryItem[] result; using (var repository = _repositoryFactory()) { //Optimize performance and CPU usage repository.DisableChangesTracking(); result = (await repository.GetPropertyDictionaryItemsByIdsAsync(ids)) .Select(x => x.ToModel(AbstractTypeFactory <PropertyDictionaryItem> .TryCreateInstance())) .ToArray(); } return result; })); }