Home > Presentation, WCF > The Smart Client Object Model: Silverlight Support

The Smart Client Object Model: Silverlight Support


We introduced the Smart Client Object Model in a previous post and we mentioned the fact that it could be used in both standard .NET apps as well as Silverlight apps. This is true thanks to Silverlight’s CoreCLR.

Starting from Silverlight 2, Silverlight ships a scaled version of the original .NET CLR named the CoreCLR. It basically is a stripped down version of the standard CLR, only containing what is needed to develop rich .NET internet applications. In a nutshell, the CoreCLR contains:

  • Most of the same Base Class Libraries as the ones provided in the standard CLR,
  • JIT,
  • Garbage Collector,
  • Security Model,
  • Exception Handling,
  • Loader & Binder,
  • Debugging APIs

Nevertheless, as you understood, not all .NET classes are available so to ensure that the generated Smart Client Object Model can be used by any Smart-Client including Silverlight ones you’ll need to reference the CodeFluent Silverlight Runtime (corresponding to your Silverlight version)which adds support for missing features required by the object model.

Furthermore, the runtime also adds a key feature which is the Silverlight Automatic Asynchronous Proxy (aka SLAAP). As you may know, the Silverlight programming model requires that all server calls cannot be issued from the UI thread and need to be asynchronous. As a consequence developers need to implement the IAsyncResult interface for all their server calls. Therefore, without using CodeFluent Entities, making a server call in Silverlight looks like something like this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    MySampleService.CustomerCollectionClient client = new MySampleService.CustomerCollectionClient();
    client.LoadAllCompleted += new EventHandler(client_LoadAllCompleted);
    client.LoadAllAsync();
}

void client_LoadAllCompleted(object sender, MySampleService.LoadAllCompletedEventArgs e)
{
    listBox1.ItemsSource = e.Result;
}

Well using CodeFluent Entities and its generated Smart Client Object Model you don’t have to bother using the IAsyncResult interface anymore with those LoadAllAsync and LoadAllCompleted members, but simply call the LoadAll method as you would have done in a standard .NET application. However, since Silverlight requires that server calls should not be issued on the UI thread you need to run it on a background thread such as this:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    ThreadPool.QueueUserWorkItem(GetCustomers);
}

private void GetCustomers(object state)
{
    CustomerCollection customers = CustomerCollection.LoadAll();
    Dispatcher.BeginInvoke(() => listBox1.ItemsSource = customers);
}

Better, but still this implies that developers have to do the QueueUserWorkItem and BeginInvoke themselves. To remedy this, the runtimes actually include two handy extensions RunAsync and UpdateUI which actually take care of all this. This way loading business data using the Smart Client Object Model in Silverlight gets down to do the following:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    this.RunAsync(() =>
    {
        // 1. business code
        CustomerCollection customers = CustomerCollection.LoadAll();

        this.UpdateUI(() =>
        {
            // 2. UI update code
            listBox1.ItemsSource = customers;
        });
    });
}

To sum-up, key points using the Smart Client Object Model in Silverlight are:

  • It’s the same object model for all smart clients,
  • this is possible mostly thanks to the runtimes which provide the missing features in the targeted CoreCLR (2, 3, 4),
  • furthermore the runtimes provide the Silverlight Automatic Asynchronous Proxy feature so you don’t have to bother with IAsyncResult anymore.
  1. No comments yet.
  1. No trackbacks yet.

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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers