LLBLGen and Pagination

Friday, July 10 2009

LLBLGen's methods provide for an easy way to fetch a subset of rows from persistence. One can limit the returned results using a filter bucket (i.e. a where clause) or by using pagination. All one has to do is give the current page and the number of records per page to LLBLGen and it handles the rest. This is the code that I wrote in order to get numberOfRecordsPerPage records for page pageNumber:

   1: int maxItemsToReturn = 0; // No limit.
   2: using (var myAdapter = PersistenceLayer.GetDataAccessAdapter())
   3: {
   4:     myAdapter.FetchTypedView(typedViewCollection.GetFieldsInfo(),
   5:         typedViewCollection, filterBucket, maxItemsToReturn, null, false, 
   6:         null, pageNumber, numberOfRecordsPerPage);
   7: }

Unfortunately, this was not limiting the returned records by any means – all of the records that fit the filter bucket were being returned and no pagination filtering was occurring. The query that LLBLGen generates shows that no limiting is being done.

I found out this morning that the maxItemsToReturn argument was overriding the pagination arguments given to the function. Since 0 was given to it, LLBLGen will not limit the returned rows by any means, despite the fact that I gave it pagination limitations. This is the only overload of FetchTypedView() available to utilize the pagination capabilities, so we’re stuck with providing some value for maxItemsToReturn. While I do not want unlimited rows returned (i.e. setting it to 0), I also do not want to return a smaller amount than expected. The fix I settled on was to set the maximum number of items to return to whatever numberOfRecordsPerPage is so that that argument does not unintentionally limit the resulting set of data:

   1: using (var myAdapter = PersistenceLayer.GetDataAccessAdapter())
   2: {
   3:     myAdapter.FetchTypedView(typedViewCollection.GetFieldsInfo(),
   4:         typedViewCollection, filterBucket, numberOfRecordsPerPage, null, false, 
   5:         null, pageNumber, numberOfRecordsPerPage);
   6: }

This generates the proper query and returns the correct result.