1. Silverlight LOB Enhancements
Silverlight 3 introduced
additional controls for line-of-business (LOB) application development.
In addition, Silverlight 3 introduced the new Silverlight Navigation
Application project template, which provides a great starter application
for a LOB application.
Silverlight 4 builds
on these capabilities with improved data-oriented controls as well as
new controls such as the RichTextBox control to display, enter, and edit
rich text and the WebBrowser control for hosting HTML within an
out-of-browser (OOB) application. Speaking of which, Silverlight 4
greatly improves OOB functionality to enhance LOB development.
HTML hosting in the WebBrowser control
Pop-up alerts with the NotificationWindow class
Support for elevated trust to the underlying platform
Other LOB-related enhancements
in Silverlight 4 are drag-and-drop, clipboard access, right-click mouse
events, and printing. A major step forward is much better support for
Commanding, which allows developers to more easily implement
Model-View-ViewModel applications when building data-driven
applications.
2. Data Access Enhancements
Silverlight 4 includes strong network service capabilities .
In this article, we highlight additional networking support for
accessing data in a structured way via both WCF Data Services and WCF
RIA Services. The next two sections provide a high-level overview of
these two technologies. For a deeper discussion on these technologies
and how they relate to other SOAP and REST-based development models,
please go to:
blogs.msdn.com/endpoint/archive/2010/01/04/wcf-data-services-
ria-services-alignment-questions-and-answers.aspx.
2.1. WCF Data Services
Silverlight 4 includes rich
support for accessing web data either on the Internet or intranet via
WCF Data Services. Formerly known as ADO.NET Data Services in
Silverlight 3, WCF Data Services provides support for REST-based data
access as well as support for the new Open Data Protocol (OData) format.
You can learn more about OData at
www.odata.org/
Silverlight 4 introduces several improvements for WCF Data Services including a new DataServiceCollection
class that provides better data binding support with automatic updates
to bound controls and greatly simplifying REST development in
Silverlight. WCF Data Services also supports both out-of-browser and
cross-domain execution in Silverlight 4.
2.2. WCF RIA Services
When you think LOB applications, you automatically think n-tier
development. Introduced at Mix 09 in beta as Microsoft .NET Rich
Internet Applications (RIA) Services, the name has changed to WCF RIA
Services but the goal is the same, which is to simplify the development
of n-tier solutions for Silverlight RIA applications. WCF RIA Services
is automatically installed when you download and install the Silverlight
4 Tools for Visual Studio 2010.
The challenge with n-tier
applications is flowing data across application tiers with support for
edits, validation, and so on.
WCF RIA Services provides a
bridge between Silverlight 4's rich presentation-tier capabilities and
ASP.NET's powerful middle-tier capabilities on the server, including
support for ASP.NET authentication and user settings management.
To get started with WCF RIA Services, you create classes in the middle-tier web application that inherit from the System.Web.DomainServices.DomainService
base class to define a set of operations on resources such as an
ADO.NET Entity Model or other resources such as a user registration
service. You can have classes on the middle tier be available on the
client-side in Silverlight by adding the EnableClientAccess
attribute to the middle-tier classes. WCF RIA Services will
automatically generate the corresponding client-side code into the
presentation tier (the Silverlight application). At this point, you can
code against the generated client-side code without worrying about
tracking and packaging up changes and so forth to pass back to the
middle tier. WCF RIA Services manages this for you.
3. Accessing RESTful Data using OData
3.1. Problem
You need to access web data in your Silverlight application.
3.2. Solution
Take advantage of existing REST and OData end-points either on the intranet or Internet.
3.3. How It Works
Silverlight 4
includes a client development model that enables accessing REST and
OData end-points by simply using the Add Service Reference feature in
Visual Studio 2010 to create the necessary client-side representations
of the server-side data.
To find your Web Data,
navigate over to the odata.org site and click Producers to see what
sample data is available. You will connect to the OData Test read-only
dataset available at
services.odata.org/OData/OData.svc/
3.4. The Code
To get started, right-click on Recipe 1 in Visual Studio and select Add Service Reference and enter the URL for the sample OData service as shown in Figure 1.
Clicking OK generates the necessary client-side code files to enable access to the service. Next, add a using reference to bring in the client-side code:
using Ch09_LOBApplications.Recipe9_1.OdataDemoReadOnlyServiceReference
using System.Data.Services.Client
Then, add a MainPage_Loaded event handler on the main page where you do three things:
Initialize the data service context to connect to the service
Create a collection to store the data
Define a handler for the LoadCompleted event for the collection when loaded
Here is the MainPage_Loaded event handler containing these steps:
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
context = new DemoService(
new Uri("http://services.odata.org/OData/OData.svc/",
UriKind.Absolute));
Products = new DataServiceCollection<Product>(context);
Products.LoadCompleted +=
new EventHandler<LoadCompletedEventArgs>(Products_LoadCompleted);
}
As you can see, you configure the context variable to point to the OData test service. Next, you instantiate a new DataServiceCollection with the Product type and assign a LoadCompleted event handler. Then you add a Button to kick off loading the data and a DataGrid to display the data in the UI. The LoadCompleted event fires when the GetProductsBtn is clicked.
In the GetProductsBtn Button event, you create a LINQ query to retrieve all of the products and then pass the query in to the LoadAsync method on the Products variable of type DataServiceCollection<Product>. The LoadAsync method uses the context variable to know where to retrieve the data from. Here is the GetProductsBtn_Click event handler:
private void GetProductsBtn_Click(object sender, RoutedEventArgs e)
{
ProductsGrid.DataContext = null;
var ProductsQuery = from products in context.Products
select products;
Products.LoadAsync(ProductsQuery);
}
When LoadAsync completes, the DataServiceCollection.LoadCompleted event is fired on the Products variable, which checks for an error; if no errors are present, it assigns the data to the ProductsGrid.ItemSource property as shown in Listing 9-1.
Listing 1. The Recipe 1 Products_LoadCompleted Method
void Products_LoadCompleted(object sender, LoadCompletedEventArgs e) { if (e.Error == null) { // Load all pages of Orders before binding. if (Products.Continuation != null)
{ Products.LoadNextPartialSetAsync(); } else { // Bind the root StackPanel element to the collection; // related object binding paths are defined in the XAML. ProductsGrid.ItemsSource = Products; ProductsGrid.UpdateLayout();
// Re-enable the button since the loading is complete. GetProductsBtn.IsEnabled = true;
// Set the focus to the first order, which loads the related items. ProductsGrid.SelectedIndex = 0; } } else { MessageBox.Show(string.Format("An error has occured: {0}", e.Error.Message)); GetProductsBtn.IsEnabled = true; } }
|
The result of this code is shown in Figure 2.