C# – Question on design of current pagination implementations

asp.net-mvccnetpagination

I have checked pagination implementations on asp.net mvc specifically and i really feel that there is something less efficient in implementations.

First of all all implementations use pagination values like below.

public ActionResult MostPopulars(int pageIndex,int pageSize)
{

}

The thing that i feel wrong is pageIndex and pageSize totally should be member of Pagination class otherwise this way looks so much functional way. Also it simplify unnecesary paramater pass in tiers of application.

Second thing is that they use below interface.

public interface IPagedList<T> : IList<T>
{
    int PageCount { get; }
    int TotalItemCount { get; }
    int PageIndex { get; }
    int PageNumber { get; }
    int PageSize { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
    bool IsFirstPage { get; }
    bool IsLastPage { get; }
} 

If i want to routing my pagination to different action so i have to create new view model for encapsulate action name in it or even controller name. Another solution can be that sending this interfaced model to view then specify action and controller hard coded in pager method as parameter but i am losing totally re-usability of my view because it is strictly depends on just one action.

Another thing is that they use below code in view

Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)

If the model is IPagedList why they don't provide an overload method like @Html.Pager(Model) or even better one is @Html.Pager(). You know that we know model type in this way. Before i was doing mistake because i was using Model.PageIndex instead of Model.PageNumber.

Another big issue is they strongly rely on IQueryable interface. How they know that i use IQueryable in my data layer ? I would expected that they work simply with collections that is keep pagination implementation persistence ignorant.

What is wrong about my improvement ideas over their pagination implementations ?
What is their reason to not implement their paginations in this way ?

Best Answer

As user8685 stated: your interface seems redundant to existing MVC stuff and principles.

Try this: information you require from IPagedList, like page index etc. should be implemented in business logic layer and fed to the view/page through a generic model that can be fed back to the server and safely cast and processed there. Why? Because what you are collecting here clearly is an input to your information system and as such belongs to layers lower than UI.

This way may not be the best way to go and is certainly not the quickest, but should make it easier to see what you really need in terms of abstraction and data architecture and thus help you remove redundancy.

Also, existing helpers often contain too much overhead for simple usage and sometimes obfuscate the big picture.