My recipe for a paged list / DataModel with strong type

I needed to create some sort of IList that can handle a data source that supports paging.

Let’s start with the basics –
our class :

public class MyPagedDataModel<T> {
 /// ... code goes here....
}

Let’s continue by allowing us to determine the page size, current page index, and our data source.

        /// <summary>
        /// how many records per page
        /// </summary>
        public virtual int PageSize { get; set; }

        /// <summary>
        /// Current page index - offset 1 (first page is 0)
        /// </summary>
        public virtual int CurrentPageIndex { get; set; }

        public virtual object DataSource { get; set; }

To determine the number of records, and pages, I will have to skip one step, I trust you can keep up, but feel free to contact me if you have questions –


private IEnumerable<T> _list;

 public virtual IEnumerable<T> DisplayItems {
            get {
                ParseData();
                return _list;
            }
        }

internal override void ParseData()
        {

            if (_list == null)
            {
                _list = (IEnumerable<T>) DataSource;
                ApplyFilters();

                if (!PrePaged)
                {
                    TotalRecords = ((IQueryable<T>) DataSource).Count();

                    _list = _list.Skip(CurrentPageIndex * PageSize).Take(PageSize);
                }

            }
            base.ParseData();
        }

/// <summary>
        /// Total data records
        /// </summary>
        public virtual int TotalRecords
        {
            get
            {
                if (_totalRecords>0)
                    return _totalRecords; 

                ParseData();
                return _totalRecords;
            }
            set
            {
                _totalRecords = value;
            }
        }

        /// <summary>
        /// Compute the pages count.
        /// </summary>
        public virtual int TotalPages{
            get {
                if (PageSize == 0)
                    return 0;

                return (int)Math.Ceiling((decimal)TotalRecords / (decimal)PageSize);
            }
        }

Let’s see the complete class

public class MyPagedDataModel<T> {
        /// <summary>
        /// how many records per page
        /// </summary>
        public virtual int PageSize { get; set; }

        /// <summary>
        /// Current page index - offset 1 (first page is 0)
        /// </summary>
        public virtual int CurrentPageIndex { get; set; }

        public virtual object DataSource { get; set; }

private IEnumerable<T> _list;

 public virtual IEnumerable<T> DisplayItems {
            get {
                ParseData();
                return _list;
            }
        }

internal override void ParseData()
        {

            if (_list == null)
            {
                _list = (IEnumerable<T>) DataSource;
                ApplyFilters();

                if (!PrePaged)
                {
                    TotalRecords = ((IQueryable<T>) DataSource).Count();

                    _list = _list.Skip(CurrentPageIndex * PageSize).Take(PageSize);
                }

            }
            base.ParseData();
        }

/// <summary>
        /// Total data records
        /// </summary>
        public virtual int TotalRecords
        {
            get
            {
                if (_totalRecords>0)
                    return _totalRecords; 

                ParseData();
                return _totalRecords;
            }
            set
            {
                _totalRecords = value;
            }
        }

        /// <summary>
        /// Compute the pages count.
        /// </summary>
        public virtual int TotalPages{
            get {
                if (PageSize == 0)
                    return 0;

                return (int)Math.Ceiling((decimal)TotalRecords / (decimal)PageSize);
            }
        }

}
As always, feel free to contact me with suggestions or questions

Speak Your Mind

*