public virtual async Task SaveDictionaryItemsAsync(DynamicPropertyDictionaryItem[] items) { if (items == null) { throw new ArgumentNullException(nameof(items)); } using (var repository = _repositoryFactory()) { var dbExistItems = await repository.GetDynamicPropertyDictionaryItemByIdsAsync(items.Where(x => !x.IsTransient()).Select(x => x.Id).ToArray()); foreach (var item in items) { var originalEntity = dbExistItems.FirstOrDefault(x => x.Id == item.Id); var modifiedEntity = AbstractTypeFactory <DynamicPropertyDictionaryItemEntity> .TryCreateInstance().FromModel(item); if (originalEntity != null) { modifiedEntity.Patch(originalEntity); } else { repository.Add(modifiedEntity); } } await repository.UnitOfWork.CommitAsync(); DynamicPropertiesCacheRegion.ExpireRegion(); } }
public virtual async Task <DynamicProperty[]> SaveDynamicPropertiesAsync(DynamicProperty[] properties) { if (properties == null) { throw new ArgumentNullException(nameof(properties)); } var pkMap = new PrimaryKeyResolvingMap(); using (var repository = _repositoryFactory()) { var dbExistProperties = (await repository.GetDynamicPropertiesForTypesAsync(properties.Select(x => x.ObjectType).Distinct().ToArray())).ToList(); foreach (var property in properties) { var originalEntity = dbExistProperties.FirstOrDefault(x => property.IsTransient() ? x.Name.EqualsInvariant(property.Name) && x.ObjectType.EqualsInvariant(property.ObjectType) : x.Id.EqualsInvariant(property.Id)); var modifiedEntity = AbstractTypeFactory <DynamicPropertyEntity> .TryCreateInstance().FromModel(property, pkMap); if (originalEntity != null) { modifiedEntity.Patch(originalEntity); } else { repository.Add(modifiedEntity); } } repository.UnitOfWork.Commit(); pkMap.ResolvePrimaryKeys(); DynamicPropertiesCacheRegion.ExpireRegion(); } return(properties); }
public virtual async Task <DynamicPropertySearchResult> SearchDynamicPropertiesAsync(DynamicPropertySearchCriteria criteria) { var cacheKey = CacheKey.With(GetType(), "SearchDynamicPropertiesAsync", criteria.GetCacheKey()); return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(DynamicPropertiesCacheRegion.CreateChangeToken()); var result = AbstractTypeFactory <DynamicPropertySearchResult> .TryCreateInstance(); using (var repository = _repositoryFactory()) { //Optimize performance and CPU usage repository.DisableChangesTracking(); var query = repository.DynamicProperties; if (!string.IsNullOrEmpty(criteria.ObjectType)) { query = query.Where(x => x.ObjectType == criteria.ObjectType); } if (!string.IsNullOrEmpty(criteria.Keyword)) { query = query.Where(x => x.Name.Contains(criteria.Keyword)); } if (!criteria.ObjectTypes.IsNullOrEmpty()) { query = query.Where(m => criteria.ObjectTypes.Contains(m.ObjectType)); } var sortInfos = criteria.SortInfos; if (sortInfos.IsNullOrEmpty()) { sortInfos = new[] { new SortInfo { SortColumn = "Name" } }; } query = query.OrderBySortInfos(sortInfos); result.TotalCount = await query.CountAsync(); if (criteria.Take > 0) { var ids = await query.Skip(criteria.Skip) .Take(criteria.Take) .Select(x => x.Id) .AsNoTracking() .ToListAsync(); var properties = await _dynamicPropertyService.GetDynamicPropertiesAsync(ids.ToArray()); result.Results = properties.OrderBy(x => ids.IndexOf(x.Id)) .ToList(); } } return result; })); }
public virtual async Task <DynamicPropertyDictionaryItem[]> GetDynamicPropertyDictionaryItemsAsync(string[] ids) { var cacheKey = CacheKey.With(GetType(), "GetDynamicPropertyDictionaryItemsAsync", string.Join("-", ids)); return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(DynamicPropertiesCacheRegion.CreateChangeToken()); using (var repository = _repositoryFactory()) { //Optimize performance and CPU usage repository.DisableChangesTracking(); var result = await repository.GetDynamicPropertyDictionaryItemByIdsAsync(ids); return result.Select(x => x.ToModel(AbstractTypeFactory <DynamicPropertyDictionaryItem> .TryCreateInstance())).ToArray(); } })); }
public virtual async Task DeleteDynamicPropertiesAsync(string[] propertyIds) { if (propertyIds == null) { throw new ArgumentNullException(nameof(propertyIds)); } using (var repository = _repositoryFactory()) { var properties = repository.DynamicProperties.Where(p => propertyIds.Contains(p.Id)) .ToList(); foreach (var property in properties) { repository.Remove(property); } await repository.UnitOfWork.CommitAsync(); DynamicPropertiesCacheRegion.ExpireRegion(); } }
public async virtual Task <DynamicProperty> GetByNameAsync(string objectType, string propertyName) { if (objectType == null) { throw new ArgumentNullException(nameof(objectType)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } var cacheKey = CacheKey.With(GetType(), nameof(GetByNameAsync)); var dict = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) => { cacheEntry.AddExpirationToken(DynamicPropertiesCacheRegion.CreateChangeToken()); var result = new List <DynamicProperty>(); var criteria = new DynamicPropertySearchCriteria { Skip = 0, Take = _pageSize }; var searchResult = await _searchService.SearchDynamicPropertiesAsync(criteria); result.AddRange(searchResult.Results); var totalCount = searchResult.TotalCount; for (var skip = _pageSize; skip < totalCount; skip += _pageSize) { criteria.Skip = skip; searchResult = await _searchService.SearchDynamicPropertiesAsync(criteria); result.AddRange(searchResult.Results); } return(result.Distinct().ToDictionary(x => $"{x.ObjectType}__{x.Name}", StringComparer.InvariantCultureIgnoreCase).WithDefaultValue(null)); }); return(dict[$"{objectType}__{propertyName}"]); }
public virtual async Task DeleteDictionaryItemsAsync(string[] itemIds) { if (itemIds == null) { throw new ArgumentNullException(nameof(itemIds)); } using (var repository = _repositoryFactory()) { var items = repository.DynamicPropertyDictionaryItems .Where(v => itemIds.Contains(v.Id)) .ToList(); foreach (var item in items) { repository.Remove(item); } await repository.UnitOfWork.CommitAsync(); DynamicPropertiesCacheRegion.ExpireRegion(); } }