PagedList Implementation in Visual Basic 9

Rob Conery first created a post about PagedList. A PagedList is like a sliced list, a predefined subset of whole items in a collection.The purpose of PagedList is for displaying paged items on a page. We often find a grid or a table that displays only 20 items of the whole items. To see the next 20 items we can click on the next link. This paging mechanism is the place where PagedList is helpfull.

PagedList implementation is often related with ASP.NET MVC project. Well, from some point of views, ASP.NET MVC can take some advantages of PagedList. In general, the PagedList can be used in any architecture including ASP.NET Web Form. Its purpose is to have a sliced result of the whole items. If you have this kind of requirement in your project, PagedList is suitable for you.

Rob's PagedList is written in C#. Here I try to migrate the source code to Visual Basic 9 with some modifications. The source code is written inline in this post. I hope this post is useful for VB.NET developers. If you want to post a feedback, please comment this post.

Imports System.Linq

Imports System.Runtime.CompilerServices

 

Public Interface IPagedList(Of T)

    Inherits IList(Of T)

 

    Property PageCount() As Integer

    Property TotalItemCount() As Integer

    Property PageIndex() As Integer

    Property PageNumber() As Integer

    Property PageSize() As Integer

    Property HasPreviousPage() As Boolean

    Property HasNextPage() As Boolean

    Property IsFirstPage() As Boolean

    Property IsLastPage() As Boolean

 

End Interface

 

Public Class PagedList(Of T)

    Inherits List(Of T)

    Implements IPagedList(Of T)

 

    Public Sub New(ByVal pSource As IEnumerable(Of T), _

                   ByVal pIndex As Integer, _

                   ByVal pPageSize As Integer)

        If (TypeOf pSource Is IQueryable(Of T)) Then

            Initialize(TryCast(pSource, IQueryable(Of T)), pIndex, pPageSize)

        Else

            Initialize(pSource.AsQueryable(), pIndex, pPageSize)

        End If

    End Sub

 

    Public Sub New(ByVal pSource As IQueryable(Of T), _

                   ByVal pIndex As Integer, _

                   ByVal pPageSize As Integer)

        Initialize(pSource, pIndex, pPageSize)

    End Sub

 

    Public Sub Initialize(ByVal pSource As IQueryable(Of T), _

                               ByVal pIndex As Integer, _

                               ByVal pPageSize As Integer)

        If pSource Is Nothing Then pSource = New List(Of T)().AsQueryable()

        TotalItemCount = pSource.Count()

        PageSize = pPageSize

        PageIndex = pIndex

        If TotalItemCount > 0 Then

            PageCount = CType(Math.Ceiling(TotalItemCount / CType(PageSize, Double)), Integer)

        Else

            PageCount = 0

        End If

        HasPreviousPage = (PageIndex > 0)

        HasNextPage = (PageIndex < (PageCount - 1))

        IsFirstPage = (PageIndex <= 0)

        IsLastPage = (PageIndex >= (PageCount - 1))

 

        If pIndex < 0 Then

            Throw New ArgumentOutOfRangeException("PageIndex cannot be below 0.")

        End If

        If (PageSize < 1) Then

            Throw New ArgumentOutOfRangeException("PageSize cannot be less than 1.")

        End If

 

        If TotalItemCount > 0 Then

            AddRange(pSource.Skip(pIndex * pPageSize).Take(pPageSize))

        Else

            AddRange(pSource.Skip(pIndex * pPageSize).Take(pPageSize))

        End If

    End Sub

 

    Private _hasNextPage As Boolean

    Public Property HasNextPage() As Boolean _

        Implements IPagedList(Of T).HasNextPage

        Get

            Return _hasNextPage

        End Get

        Private Set(ByVal value As Boolean)

            _hasNextPage = value

        End Set

    End Property

 

    Private _hasPreviousPage As Boolean

    Public Property HasPreviousPage() As Boolean _

        Implements IPagedList(Of T).HasPreviousPage

        Get

            Return _hasPreviousPage

        End Get

        Private Set(ByVal value As Boolean)

            _hasPreviousPage = value

        End Set

    End Property

 

    Private _isFirstPage As Boolean

    Public Property IsFirstPage() As Boolean _

        Implements IPagedList(Of T).IsFirstPage

        Get

            Return _isFirstPage

        End Get

        Private Set(ByVal value As Boolean)

            _isFirstPage = value

        End Set

    End Property

 

    Private _isLastPage As Boolean

    Public Property IsLastPage() As Boolean _

        Implements IPagedList(Of T).IsLastPage

        Get

            Return _isLastPage

        End Get

        Private Set(ByVal value As Boolean)

            _isLastPage = value

        End Set

    End Property

 

    Private _pageCount As Integer

    Public Property PageCount() As Integer _

        Implements IPagedList(Of T).PageCount

        Get

            Return _pageCount

        End Get

        Private Set(ByVal value As Integer)

            _pageCount = value

        End Set

    End Property

 

    Private _pageIndex As Integer

    Public Property PageIndex() As Integer _

        Implements IPagedList(Of T).PageIndex

        Get

            Return _pageIndex

        End Get

        Private Set(ByVal value As Integer)

            _pageIndex = value

        End Set

    End Property

 

    Private _pageNumber As Integer

    Public Property PageNumber() As Integer _

        Implements IPagedList(Of T).PageNumber

        Get

            Return _pageNumber

        End Get

        Private Set(ByVal value As Integer)

            _pageNumber = value

        End Set

    End Property

 

    Private _pageSize As Integer

    Public Property PageSize() As Integer _

        Implements IPagedList(Of T).PageSize

        Get

            Return _pageSize

        End Get

        Private Set(ByVal value As Integer)

            _pageSize = value

        End Set

    End Property

 

    Private _totalItemCount As Integer

    Public Property TotalItemCount() As Integer _

        Implements IPagedList(Of T).TotalItemCount

        Get

            Return _totalItemCount

        End Get

        Private Set(ByVal value As Integer)

            _totalItemCount = value

        End Set

    End Property

End Class

 

Public Module Pagination

    <Extension()> _

    Public Function ToPagedList(Of T)(ByVal pSource As IQueryable(Of T), _

                                      ByVal pIndex As Integer, _

                                      ByVal pPageSize As Integer) _

        As PagedList(Of T)

 

        Return New PagedList(Of T)(pSource, pIndex, pPageSize)

 

    End Function

 

    <Extension()> _

    Public Function ToPagedList(Of T)(ByVal pSource As IEnumerable(Of T), _

                                      ByVal pIndex As Integer, _

                                      ByVal pPageSize As Integer) _

        As PagedList(Of T)

 

        Return New PagedList(Of T)(pSource, pIndex, pPageSize)

 

    End Function

End Module

Share this post: | | | |
Published Friday, April 24, 2009 6:11 AM by Agus Suhanto
Filed under: ,

Comments

# Test SyntaxHighlighter di Geeks Blog

Monday, July 27, 2009 7:36 PM by Agus Suhanto

Posting ini saya tujukan untuk nge-test syntax highlighter yang diterapkan dalam salah satu theme CS

Powered by Community Server (Commercial Edition), by Telligent Systems