示例#1
0
        public async Task <QueryResult <Ürün> > ListAsync(ÜrünlerQuery query)
        {
            IQueryable <Ürün> queryable = _context.Ürünler
                                          .Include(p => p.Kategori)
                                          .AsNoTracking();

            // AsNoTracking tells EF Core it doesn't need to track changes on listed entities. Disabling entity
            // tracking makes the code a little faster
            if (query.CategoryId.HasValue && query.CategoryId > 0)
            {
                queryable = queryable.Where(p => p.CategoryId == query.CategoryId);
            }

            // Here I count all items present in the database for the given query, to return as part of the pagination data.
            int totalItems = await queryable.CountAsync();

            // Here I apply a simple calculation to skip a given number of items, according to the current page and amount of items per page,
            // and them I return only the amount of desired items. The methods "Skip" and "Take" do the trick here.
            List <Ürün> Ürünler = await queryable.Skip((query.Page - 1) *query.ItemsPerPage)
                                  .Take(query.ItemsPerPage)
                                  .ToListAsync();

            // Finally I return a query result, containing all items and the amount of items in the database (necessary for client-side calculations ).
            return(new QueryResult <Ürün>
            {
                Items = Ürünler,
                TotalItems = totalItems,
            });
        }
示例#2
0
        private string GetCacheKeyForÜrünlerQuery(ÜrünlerQuery query)
        {
            string key = CacheKeys.ÜrünListesi.ToString();

            if (query.CategoryId.HasValue && query.CategoryId > 0)
            {
                key = string.Concat(key, "_", query.CategoryId.Value);
            }

            key = string.Concat(key, "_", query.Page, "_", query.ItemsPerPage);
            return(key);
        }
示例#3
0
        public async Task <QueryResult <Ürün> > ListAsync(ÜrünlerQuery query)
        {
            // Here I list the query result from cache if they exist, but now the data can vary according to the category ID, page and amount of
            // items per page. I have to compose a cache to avoid returning wrong data.
            string cacheKey = GetCacheKeyForÜrünlerQuery(query);

            var Ürünler = await _cache.GetOrCreateAsync(cacheKey, (entry) => {
                entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
                return(_ÜrünRepository.ListAsync(query));
            });

            return(Ürünler);
        }