You are here: Best Practices > Paging

Paging

Currently db4o doesn't provide any paging mechanism at all. However all db4o query results are lazy loaded. db4o returns a result list which only contains the ids of the objects and will load the object as soon as you access it. This means you can page by only accessing the indexes of the range you're interested in.

You can access the list directly by the indexes to get the right objects. With this you can build a paging-utility methods which start at a certain index and return a certain amount of objects. Take a look a this example utility methods.

Note that LINQ brings already methods for paging with it. The skip and take methods are optimal for implementing a paging mechanism.

public static IList<T> Paging<T>(IList<T> listToPage, int limit)
{
    return Paging(listToPage, 0, limit);
}

public static IList<T> Paging<T>(IList<T> listToPage, int start, int limit)
{
    if (start > listToPage.Count)
    {
        throw new ArgumentException("You cannot start the paging outside the list." +
                                    " List-size: " + listToPage.Count + " start: " + start);
    }
    int end = calculateEnd(listToPage, start, limit);
    IList<T> list = new List<T>();
    for (int i = start; i < end; i++)
    {
        list.Add(listToPage[i]);
    }
    return list;
}

private static int calculateEnd<T>(IList<T> resultList, int start, int limit)
{
    int end = start + limit;
    if (end >= resultList.Count)
    {
        return resultList.Count;
    }
    return end;
}
PagingUtility.cs: Paging utility methods
Public Shared Function Paging(Of T)(ByVal listToPage As IList(Of T), ByVal limit As Integer) As IList(Of T)
    Return Paging(listToPage, 0, limit)
End Function

Public Shared Function Paging(Of T)(ByVal listToPage As IList(Of T), ByVal start As Integer, ByVal limit As Integer) As IList(Of T)
    If start > listToPage.Count Then
        Throw New ArgumentException("You cannot start the paging outside the list." & " List-size: " & listToPage.Count & " start: " & start)
    End If
    Dim endPosition As Integer = calculateEnd(listToPage, start, limit)
    Dim list As IList(Of T) = New List(Of T)()
    For i As Integer = start To endPosition - 1
        list.Add(listToPage(i))
    Next
    Return list
End Function

Private Shared Function calculateEnd(Of T)(ByVal resultList As IList(Of T), _
         ByVal start As Integer, ByVal limit As Integer) As Integer
    Dim endPosition As Integer = start + limit
    If endPosition >= resultList.Count Then
        Return resultList.Count
    End If
    Return endPosition
End Function
PagingUtility.vb: Paging utility methods

And then of course you can use the utility methods on the result-sets of db4o.

IList<StoredItems> queryResult = container.Query<StoredItems>();
IList<StoredItems> pagedResult = PagingUtility.Paging(queryResult, 2, 2);
TestPagingUtility.cs: Use the paging utility
Dim queryResult As IList(Of StoredItems) = container.Query(Of StoredItems)()
Dim pagedResult As IList(Of StoredItems) = PagingUtility.Paging(queryResult, 2, 2)
TestPagingUtility.vb: Use the paging utility