Loading of collections in the desktop client on a ‘need to see’ basis.

I just spent my Saturday afternoon on a really fun puzzle: loading data on a ‘need to see’ basis. This code hasn’t been production tested at all (use at own risk), but is way too fun not to share.


The LightSwitch desktop application can sometimes feel really sluggish, this is partly because collections on a screen ‘execute automatically by default’, this means that they are loaded automatically if they are used in the UI, even if that part of the UI is not visible (for example if the grid is on a tab that is not active at the moment). So here’s the challenge: can we speed up the application by loading data on a ‘need to see’ basis? Can we make our application intelligent enough so that data is loaded if and only if it needs to be visualized?

I have a screen that contains a couple of collections, let’s say that Foo has a one-to-many relationship with Bar and I made a list-detail screen with three tabs, one containing the Foo’s details, one containing the Foo.Bars as a Data Grid, and one containing the Foo.Bars as a List.Image 247Or at runtime:Image 248

When I go to the Foos List Detail, using Fiddler or another network sniffing tool, I can see that both the Foos, and the first Foo’s.Bars are automatically loaded.Image 251

(Well: obviously, because in the screen designer both the Foo and Bars collection have their ‘Auto Execute Query’ selected…)Image 243

Making your application smarter

Add a new class to your client project, named ‘LoadOnDemandExtensions’. Copy the source for that class from this gist.  It’s in C#, but here’s a good link for our VB friends.

(For those of you that actually clicked the VB link, I’m sorry. That was mean. To make it up, I posted the VB code at the bottom of that same gist 😉 )

Step 2: right-click on the client code in Solution Explorer, hit ‘View Application Code’ to open your client application code. In the Application_Initialize method, write:

    public partial class Application
        partial void Application_Initialize()
    Public Class Application

        Private Sub Application_Initialize() 
        End Sub
    End Class

End of blog post.

No, really!  Press F5 and see how your application is behaving when it starts up now:Image 249

Even though the ‘Auto Execute Query’ is still on for Bars, the Lightswitch application didn’t load them because they aren’t visible at the moment. As soon as you hit the ‘Bars’ tab:

Image 250

 What is this sorcery?

The LoadVisualsOnDemand method will dig in the LightSwitch extensibility model to grab a hold of the NotificationService. It’ll ask that NotificationService to be notified whenever a screen is opened.

When a screen is opened it’ll loop over every collection on that screen and use some nifty hacking techniques (reflection is forbidden in SL in-browser sandboxed applications, but there’s a little loophole) to turn off ‘Auto Execute Query’ on the loader of each collection.

Also, it will search the screen for any ‘ContentItem’ (that is the right-hand side of your screen designer, which looks like a ‘visual tree’) that is displaying a collection (in reality this’ll be your List, Data Grid, or SuperDynamicGrid really). It will ask to be notified whenever such a control becomes visible/hidden. As soon as this happens, it’ll re-hack the VisualCollection to turn the ‘Auto Execute Query’ back on/off based on visibility.

This results in your collection becoming loaded, working (apparently paging, sorting and quicksearch do not work using other screen performance tips) and staying in sync (for example, you click on another Foo, this causes the Bars to be reloaded)

tl;dr; LightSwitch will now ignore your ‘Auto Execute Query’ attempts and turn that on/off depending on visibility.

Noteworthy caveat

Bugs incoming! This method will control the ‘Auto Execute Query’ property on all collections, every screen. However, some of your code might already depend on the implicit assumption that a screen collection is loaded. Screen your screen code files, and if you spot a spot where you access a collection in code, make sure it’s loaded using this other nifty extension method.

        partial void Method_Execute()
            var count = this.Bars.Count; //for example
        Private Sub Method_Execute()
            Dim count = Me.Bars.Count 'for example
        End Sub

But what if…

What if I don’t want this behavior on every screen?

Instead of turning this on on an application level, you can activate it on a screen-by-screen basis.

On those screens where you need it, turn off the ‘Auto Execute Query’ manually on each collection. Then, write this in the screens _Activated code:

        partial void FoosListDetail_Activated()
        Private Sub FoosListDetail_Activated()
        End Sub

What if I don’t want this behavior on every collection?

Instead of turning this on on an application or on a screen level, you can activate it on a collection-by-collection basis.

On those screens where you need it, turn off the ‘Auto Execute Query’ manually on those collections. Then, write this in the screens _Activated code (repeat for every collection):

        partial void FoosListDetail_Activated()
        Private Sub FoosListDetail_Activated()
        End Sub


Keep rocking LS!

(But only the visible parts)





8 thoughts on “Loading of collections in the desktop client on a ‘need to see’ basis.

      • Getting better and better. It works well. I think I have to contend with a couple of issues with -= ControlAvailable() because it seems that some of my events are not running. I will post findings.

  1. What about AutoCompleteBoxes on the form and as cells in a Grid?
    Will they load their list collections on demand too?

    • Hey Simon!
      That’s very insightful. If the AutoCompleteBoxes have their itemssource set as ‘Auto’, then they’ll behave just as normal.
      If they don’t, well hmmm I haven’t tested that scenario. Will get to it next weekend!

  2. Hi Jan, thanks for for this. I have only tested the LoadOnDemand for selected collections in a screen and here is what I have found thus far:

    1) In line 42 of the VB code, I kept getting “Invalid reference” to a null objects, perhaps you can change it to
    Dim screenCollectionProperty = TryCast(screen.Details.Properties.All().Single(Function(p) p isnot nothing AndAlso p.Value.Equals(collection)), IScreenCollectionProperty)

    2) In my test, I got an “Invalid cross-thread access” error in the FindContentItems Sub (line 139 in VB). Strangely, as I step through the code, the error only appears when it reaches the first Datagrid in the iContentItem tree

    • I went around the “Invalid cross-thread access” with a not so elegant approach. I had to manually do a FindControl for each Datagrid that uses the collection, and add the handlers for proxy_ControlAvailable and proxy_ControlUnavailable.

      Also, in the VB code, I changed “Function(p) p.Value.Equals(collection)” to “Function(p) p IsNot Nothing AndAlso p.Value.Equals(collection)”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s