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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube