<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Best Practices" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Paging</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../Resources/Code/prettify.js"> </script> <script src="../Resources/Code/lang-vb.js"> </script> <script src="../Resources/Code/jquery.min.js"> </script> <script src="../Resources/Code/ZeroClipboard.js"> </script> <script src="../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#best_practises/paging.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../best_practises.htm">Best Practices</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Paging</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor100"></a>Paging</h1> <p>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.</p> <p MadCap:conditions="Primary..NET">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.</p> <p MadCap:conditions="Primary..NET">Note that LINQ brings already methods for paging with it. The skip and take methods are optimal for implementing a paging mechanism. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">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; } </pre> <div class="codesnippet-meta">PagingUtility.cs: Paging utility methods <div class="codedownload"><a href="../CodeExamples/strategies/paging/Example-strategies-paging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">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 </pre> <div class="codesnippet-meta">PagingUtility.vb: Paging utility methods <div class="codedownload"><a href="../CodeExamples/strategies/paging/Example-strategies-paging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p>And then of course you can use the utility methods on the result-sets of db4o. </p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IList<StoredItems> queryResult = container.Query<StoredItems>(); IList<StoredItems> pagedResult = PagingUtility.Paging(queryResult, 2, 2);</pre> <div class="codesnippet-meta">TestPagingUtility.cs: Use the paging utility <div class="codedownload"><a href="../CodeExamples/strategies/paging/Example-strategies-paging-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim queryResult As IList(Of StoredItems) = container.Query(Of StoredItems)() Dim pagedResult As IList(Of StoredItems) = PagingUtility.Paging(queryResult, 2, 2)</pre> <div class="codesnippet-meta">TestPagingUtility.vb: Use the paging utility <div class="codedownload"><a href="../CodeExamples/strategies/paging/Example-strategies-paging-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p> </p> <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>