Silverlight paging

Silverlight paging

Old forum URL: forums.lhotka.net/forums/t/7392.aspx


James Thomas posted on Tuesday, August 04, 2009

Hi,

Are there any examples of paging using CslaDataProviders that someone can point me to? I don't need anything particularly complex - and can handle the SQL of paging within the database, so am really interested in the business object - UI layers. I'm looking enviously at some of the solutions available using the DataPager and RIA, and wonder whether CSLA can do something similar. e.g.:

http://blogs.telerik.com/vladimirenchev/posts/09-04-16/full_support_for_domaindatasource_with_radgridview_for_silverlight_3.aspx

(Apparently the DataPager uses something called the IPagedViewCollection interface, but I can't see this documented anywhere.)

Thanks for any pointers.

James.

RockfordLhotka replied on Wednesday, August 05, 2009

This isn't an area I've had time to explore yet.

However, keeping in mind that we're dealing with an n-tier world, the solution must be generally the same as what I did in ASP.NET.

In other words, the UI provides some set of information - at least the page number and page size - through some mechanism, perhaps the IPagedViewCollection interface.

When the UI wants another page it calls a method of some sort. That method must take the page number and page size and pass them to a static factory method on the object to get the next page. The page number and page size become criteria, and are often passed through the data portal to the server so the DAL can retrieve the right page.

Presumably the IPagedViewCollection concept is n-tier and async aware, since this operation will be async regardless of what technology is used - all server calls in SL are async.

mbblum replied on Wednesday, August 05, 2009

Paged CSLA objects are not that difficult. In the past I built them using stored procs. Now you can probably use LINQ.

Here is a summary of the steps I used with sproc in SQL Server.
For the sproc params, included the following:
@PageNum INT = 1,
@PerPage INT = 25,
@CurrentPage INT OUT,
@TotalPages INT OUT,
@TotalRows INT OUT

Within the sproc here are some key SQL:

declare @RowsToRetrieve int

SELECT @TotalRows = count(*)
from table
where {same conditions as actual search}

set @TotalPages = @TotalRows/@PerPage

if @PageNum < 1
set @PageNum = 1

if @TotalRows % @PerPage != 0
set @TotalPages = @TotalPages + 1

set @RowsToRetrieve = @PerPage * @PageNum
set @CurrentPage = @PageNum

WITH SearchResults AS
(
Select E.EventID
{... other columns}
, ROW_NUMBER() OVER(order by D.LastName, D.FirstName, D.MiddleName) 'RowNum'
from table
where {actual search}
)
Select SR.EventID
{... other columns}
-- , SR.RowNum
FROM SearchResults SR
WHERE SR.RowNum > @RowsToRetrieve - @PerPage
AND SR.RowNum <= @RowsToRetrieve

As you should see, using WITH and ROW_NUMBER() is important in this approach.

The paging information is in properties of the collection or list object, read the OUT parameters to get the values. Populate the business objects as usual for a collection. Including SR.RowNum in the results was useful during development to see what was happening, but did not need in my final output.

This limits network traffic to specified number of rows. Trade off is for complex searches, it could put a significant load on the database server.

This is one approach that works with CSLA, but certainly not the only one.

Hope that is helpful.

James Thomas replied on Wednesday, July 07, 2010

Yes - very useful. And sorry for the slow answer!

Thanks, James.

RockfordLhotka replied on Wednesday, July 07, 2010

There's also the PagedList sample in CslaLight\cs that shows how to do an async paged list - and while it is a SL sample, the same technique should work on WPF and WinForms too.

James Thomas replied on Tuesday, July 20, 2010

Hi Rocky,

Thanks for your answer. I can't see the PagedList sample though - is it called something else (or has been removed)?

Thanks, James.

RockfordLhotka replied on Tuesday, July 20, 2010

I think I wrote it for CSLA 4 - it may not be in earlier versions - though the concepts should work in 3.8 as well.

James Thomas replied on Thursday, July 22, 2010

Thanks - got it now - useful to see a clean implementation like this.

Have you considered implementing IPagedCollectionView into a base list object? I'm currently working on a poor implementation of this in order to take advantage of Silverlight controls such as the DataPager, so I'm sure others will be needing similar functionality.

James.

RockfordLhotka replied on Thursday, July 22, 2010

I hesitate to add too much into core CSLA, since I am not confident that Microsoft won't come along with a solution or interface as part of .NET/SL. I'd hate to take people down one road and then have Msft move in a different direction.

It was an easy choice to embed some support for Web Forms, because Msft did define a model in that space. But since there's no formal model elsewhere yet, it is better to keep the implementations out of core CSLA I think.

James Thomas replied on Sunday, July 25, 2010

Yes, I can see that makes sense (!). I guess I was assuming it came under the same category as implementing INotifyCollectionChanged (which we couldn't do without). I've hacked a solution together (that works Smile), but would have found things much simpler if IPagedCollectionView had also been part of CSLA. Something to keep an eye on maybe, if more 3rd party visual components emerge that assume these interfaces are implemented?

Best wishes, James.

RockfordLhotka replied on Monday, July 26, 2010

The thing with IPagedCollectionView is that it is a view construct. It isn't the kind of interface BLB would implement, for example.

Which isn't to say it couldn't somehow be part of CSLA - perhaps kind of like the CSLA 4 LinqObservableCollection or something like that.

James Thomas replied on Tuesday, July 27, 2010

Makes sense. At least there's a thread on the subject now for other people searching for PagedCollectionView Smile

Copyright (c) Marimer LLC