public static void Configure <T>(this ISupportPaging grid,
                                         IQueryable <T> query,
                                         Func <int> totalRowCount,
                                         int pageSize = 100)
        {
            ValidateInput(grid, query, totalRowCount, pageSize);

            grid.PageSize = pageSize;

            grid.OnGotoFirst = () => grid.SetDataSource(First, query);

            grid.OnGotoPrev = () => grid.SetDataSource(Prev, query);
            grid.OnGotoNext = () => grid.SetDataSource(Next, query);
            grid.OnGotoLast = () => grid.SetDataSource(Last, query);

            if (grid.TotalPages == null)
            {
                //local copy to avoid possible multiple enumerations.
                var total = totalRowCount();
                //
                grid.TotalPages = () => CalculatePages(grid, total);
            }

            //Goes to the first page.
            grid.SetDataSource(First, query);
            grid.CleanUp = () =>
                           PagingCache.Remove(grid);//<= to avoid memory  leaks.

            if (grid.EndConfigure != null)
            {
                grid.EndConfigure();
            }
            grid.Configured = true;
        }
 static IEnumerable <T> Prev <T>(this ISupportPaging grid, IQueryable <T> dataSource)
 {
     if (grid.CurrentPage != 0)
     {
         grid.CurrentPage--;
     }
     return(grid.GetPage(dataSource));
 }
 static IEnumerable <T> Last <T>(this ISupportPaging grid, IQueryable <T> dataSource)
 {
     if (grid.TotalPages() > 0)
     {
         grid.CurrentPage = grid.TotalPages() - 1;
     }
     return(grid.GetPage(dataSource));
 }
 static IEnumerable <T> Next <T>(this ISupportPaging grid, IQueryable <T> dataSource)
 {
     if (grid.CurrentPage < grid.TotalPages() - 1)
     {
         grid.CurrentPage++;
     }
     return(grid.GetPage(dataSource));
 }
            internal static void Remove(ISupportPaging grid)
            {
                var key = new CacheKey(grid);

                if (_storage.ContainsKey(key))
                {
                    _storage.Remove(key);
                }
            }
        static void SetDataSource <T>(this ISupportPaging grid, Func <ISupportPaging, IQueryable <T>, IEnumerable <T> > gotoPage, IQueryable <T> source)
        {
            grid.DataSource = null;
            grid.DataSource = QueryToSortableDataSource(gotoPage(grid, source));

            if (grid.OnPageChange != null)
            {
                grid.OnPageChange();
            }
        }
        private static int CalculatePages(ISupportPaging grid, int total)
        {
            var result = total / grid.PageSize;

            if (total % grid.PageSize != 0)
            {
                result++;
            }
            return(result);
        }
            private static IEnumerable <T> GetCached <T>(ISupportPaging grid)
            {
                var key = new CacheKey(grid);

                if (!_storage.ContainsKey(key))
                {
                    return(null);
                }
                return((IEnumerable <T>)_storage[key]);
            }
            private static void Cache <T>(ISupportPaging grid, IEnumerable <T> queryResult)
            {
                var key = new CacheKey(grid);

                if (_storage.ContainsKey(key))
                {
                    _storage.Remove(key);
                }
                _storage.Add(key, queryResult);
            }
        public static void Search <T>(this ISupportPaging grid,
                                      IQueryable <T> source,
                                      Func <T, bool> criteria)
        {
            if (!grid.Configured)
            {
                throw new InvalidOperationException(
                          "You have to configure the grid first. Take a look at the configure method");
            }

            PagingCache.Invalidate();
            grid.SetDataSource(First, source.Where(criteria).AsQueryable());
        }
Example #11
0
        public static PageInfo?BuildPage(this ISupportPaging pagingProvider, int?totalRowCount = null)
        {
            if (pagingProvider.Pagination == null)
            {
                return(null);
            }

            var pagination = pagingProvider.Pagination;

            return(new PageInfo(pagination.PageIndex, totalRowCount)
            {
                PageSize = pagination.PageSize
            });
        }
            public static IEnumerable <T> CacheAndReturn <T>(ISupportPaging grid,
                                                             IQueryable <T> dataSource)
            {
                var result = GetCached <T>(grid);

                if (result == null)
                {
                    Cache(grid,
                          //paging in action
                          (from p in dataSource select p)
                          .Skip(/*start page*/ grid.CurrentPage * grid.PageSize)
                          .Take(grid.PageSize)
                          .ToList());     //only at this point we hit the database
                }
                //***********************************************************
                return(GetCached <T>(grid));
            }
        private static void ValidateInput <T>(ISupportPaging grid, IQueryable <T> query, Func <int> totalRowCount, int pageSize)
        {
            if (grid == null)
            {
                throw new ArgumentNullException("grid");
            }

            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            if (totalRowCount == null)
            {
                throw new ArgumentNullException("totalRowCount");
            }

            if (pageSize < 1)
            {
                throw new ArgumentException("Page size must be greater than zero.");
            }
        }
Example #14
0
        public static IQueryable <T> ApplyPaging <T>(this ISupportPaging pagingProvider, IQueryable <T> query)
        {
            if (pagingProvider.Pagination == null)
            {
                return(query);
            }

            var pagination = pagingProvider.Pagination;

            if (pagination.PageSize.HasValue &&
                pagination.PageIndex.HasValue)
            {
                var skip = (pagination.PageIndex.Value - 1) * pagination.PageSize.Value;
                var take = pagination.PageSize.Value;

                query = query
                        .Skip(skip)
                        .Take(take);
            }

            return(query);
        }
 static IEnumerable <T> GetPage <T>(this ISupportPaging grid,
                                    IQueryable <T> dataSource)
 {
     return(PagingCache.CacheAndReturn(grid, dataSource));
 }
        //

        private static IEnumerable <T> InternalSearch <T>(this ISupportPaging grid, IQueryable <T> dataSource)
        {
            grid.CurrentPage = 0;
            PagingCache.Invalidate();//maybe this is not the best way to go....
            return(grid.GetPage(dataSource));
        }
 public CacheKey(ISupportPaging grid)
 {
     _grid     = grid;
     _gridPage = grid.CurrentPage;
 }
 //paging strategies.
 static IEnumerable <T> First <T>(this ISupportPaging grid,
                                  IQueryable <T> dataSource)
 {
     grid.CurrentPage = 0;
     return(grid.GetPage(dataSource));
 }