Ejemplo n.º 1
0
        public virtual async Task <GenericSearchResult <DynamicProperty> > SearchDynamicPropertiesAsync(DynamicPropertySearchCriteria criteria)
        {
            var cacheKey = CacheKey.With(GetType(), "SearchDynamicPropertiesAsync", criteria.GetHashCode().ToString());

            return(await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                var result = new GenericSearchResult <DynamicProperty>();
                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));
                    }

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] { new SortInfo {
                                                SortColumn = "Name"
                                            } };
                    }
                    query = query.OrderBySortInfos(sortInfos);
                    result.TotalCount = await query.CountAsync();
                    var ids = await query.Skip(criteria.Skip)
                              .Take(criteria.Take)
                              .Select(x => x.Id)
                              .ToListAsync();

                    var properties = await _dynamicPropertyService.GetDynamicPropertiesAsync(ids.ToArray());
                    result.Results = properties.OrderBy(x => ids.IndexOf(x.Id))
                                     .ToList();
                }
                return result;
            }));
        }