Wahlin on .NET

Data Access Options in Silverlight 2

Silverlight 2 lets you access and manipulate data several ways, thanks to a data access framework that's built right in.

Data is king in today's business applications. After all, without data there'd be no need to write applications in many cases because there'd be nothing to show end users. So it follows that for business applications to succeed, they must have a framework in place to access, manipulate and bind data.

Enter Silverlight 2, which can be used to build business applications that access and interact with a variety of data formats. In this column, I'll introduce the data access options available in Silverlight 2 and give an overview of how the data access framework built into Silverlight works. Then in future columns, I'll drill into different data access options and demonstrate how they can be used to integrate business data into your applications.

Before getting started, it's important to understand that Silverlight runs within a sandboxed environment, making it subject to the same rules that apply to browser-based applications. As a result, you can't use Silverlight 2 to call a database directly using standard ADO.NET classes that you've used in ASP.NET or Windows Forms applications. However, remote databases can be queried by calling services that expose this data.

The Asynchronous Model
Silverlight relies on an asynchronous model to retrieve data from remote sources. In a nutshell, this means that a separate thread is used to access data so that the user interface doesn't lock up as the user interacts with it during a data call. The user interface runs on one thread while the data access call runs on another.

Although leveraging the asynchronous model available in Silverlight requires no expertise, you do have to understand how to initiate a call, handle events and process the data once it returns. Some of the data access classes available in Silverlight automatically route data retrieved from a remote service back to the user interface for consumption, while others require you to manually route the data to the UI after it's retrieved.

Regardless of which Silverlight data access class you rely on in your applications, you'll always deal with an event-driven asynchronous model that's quite straightforward to use. Here are the basic steps you'll follow the majority of the time to work with Silverlight's asynchronous model:

  1. Create an instance of the data access/networking class that you'd like to use to retrieve data.
  2. Handle the object's completed event and hook it to an event handler.
  3. Start the asynchronous call using the object created in Step 1.
  4. Once the call completes and data is returned, the event handler defined in Step 2 will be called and you can process and bind the data.
Data Access Options
Silverlight 2 provides several different classes that can be used to access data from distributed services, such as Web Services and Representation State Transfer (REST) APIs. If you'll be accessing data provided by Web Services (like ASP.NET's ASMX service or other standard services), WCF or ADO.NET Data Services, you can use a proxy object created directly in Visual Studio 2008. This makes it easy to call services without having to write a lot of custom code.

Silverlight 2 can also be used to call REST APIs, such as those exposed at Flickr.com. A REST API is called through a URL and can return a variety of data formats such as XML and JSON. REST APIs can be accessed using classes such as WebClient and HttpWebRequest/HttpWebResponse. Each of these classes is located in the System.Net namespace in the Silverlight framework.

WebClient is arguably the easiest class to use because it can access data and pass back strings that can be parsed and manipulated. Data retrieved from a remote service using WebClient is automatically routed back to the user interface for consumption and data binding. The HttpWebRequest/HttpWebResponse classes provide a way to work with high-performance data streams. When using these classes, you'll have to write code to process the data streams and route the data back to the user interface.

In the next column, I'll demonstrate how to use the WebClient class. But in the meantime, here's a quick code sample of this class in action that follows the asynchronous steps I showed earlier:

private void  StartWebClient()
{
WebClient restClient = new WebClient();
   restClient.DownloadStringCompleted +=
     new DownloadStringCompletedEventHandler(
      restClient_DownloadStringCompleted);
   restClient.DownloadStringAsync(restURL);
}

private void restClient_DownloadStringCompleted(object sender,
  DownloadStringCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
   {
   ProcessData(e.Result);
   }
}

Stay tuned for more details!

About the Author

Dan Wahlin (Microsoft MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.

comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube