Tuesday 14 August 2012

Data virtualization in Silverlight

When I first went freelance (15 months ago – how time has rocketed by!) I promised myself that, with the reigns of my schedule tightly held in my own fist, I would dedicate more time to blogging. Well! You can all see from my blog archives how that turned out. I have discovered that when you have clients willing to pay for as much time as you can give them, there’s a strange temptation to give them as much time as you’ve got!

However.

Just before I went on vacation last week, I found made the time to write a mini-blizzard of blog posts, though not for Functional Fun. Ayende, my client for the last few months, has published them on his company’s blog. In the first batch, I wrote about some exciting new features I’ve coded up for the user interface of RavenDb, Hibernating Rhino’s Document Database server.

But I think you’ll most enjoy a couple of posts I wrote about data virtualization in Silverlight. The problem is a common one. How do you display huge lists of data items in a client application without pulling data unnecessarily from the server? Often applications fall back on paging – show one page of data, and make the user click a Next button when they want to see more. But in terms of usability, this technique feels pretty neolithic. So I figured out a way of using ListBoxes and DataGrids without needing to load all the data up front, but with properly proportioned scrollbars, slurping data from the server just in time as the user scrolls.

Over on Hibernating Rhino’s blog you’ll find the two posts I wrote about this:

There’s also a bonus post on how I created a VirtualizingWrapPanel for Silverlight – built so that we could display data in a card view rather than a list view.

And best of all, I published a sample application on GitHub with all the code you need to use this technique in your own applications. For those WPF-aficionados amongst you, you should find that the code translates without much difficulty.

10 comments:

Krunal Chaudhari said...

Hi Sam,

Couldn't you have used http://blog.landdolphin.net/?p=78 instead of reinventing the wheel?

Samuel Jack said...

Thanks for the link.

But I think my wheel is rounder than theirs :-)

If you look at the details of that post, you'll see that it doesn't do true data virtualisation: it creates a dummy item for every data item in the list up front. This would be problematic for very large lists. My implementation only creates virtual items as you scroll, so it could handle billions of data points (int.MaxValue) without a problem.

Krunal Chaudhari said...

I see. Cool. Just thought of sharing it with you. By the way yesterday
when I tried to download your code from github I received. Some github
problem...will try today again. Thank you again!

djerry said...

Any idea how I could use your idea in SL 4? Tasks are only implemented in SL 5 (which is the target version in your example). It seems that would be the only problem.

Samuel Jack said...

Robert McLaws has created a NuGet which implements the Task Parallel Library for SL4. Get it here
http://nuget.org/packages/System.Threading.Tasks. That should get you up and running.

djerry said...

That worked nice, got across that before, but wasn't sure this was gonna do the work. On another note, does this solution allow an easy transition to another datasource (instead of netflix, a SQL Server datasource)?

Samuel Jack said...

So long as the data source will allow you to fetch data in pages, it should work fine. You just need to implement IVirtualCollectionSource for your specific data source.

Jamie Lewis said...

Really like this piece of work. Unfortunately Netflix have suspended their odata service, so the demo no longer works. Any chance you could re-work using another odata source, http://services.odata.org/Northwind/Northwind.svc/ for example. I tried doing this myself but could not get it to work. Better still, could you do an example using a simulated data source located in the VirtualCollection.Web project?

Samuel Jack said...

Jamie,
Thanks for alerting me to this. I've just pushed an update to the GitHub repo so that it now uses the Northwind database. Not so exciting as scrolling through movies, but it still shows the point.

Eriani said...

Hello,
does it work with another webservice such as Asmx or WCF? Is it possible to edit datagrid's rows?
My Application is SL 4/5 using asmx webservices, I've big editable datagrid so your solution may be usful for me
Jérôme

Post a Comment