예제 #1
0
        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;
            }));
        }
예제 #2
0
        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();
                }
            }));
        }
예제 #3
0
        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}"]);
        }