BANGALORE, INDIA: Last month, we looked at how you can create a business application using Silverlight 3 and a WCF service to talk to and from a database hosted on the server. This method is quite okay when creating a simple business application that needs to talk to the database to retrieve or modify the data in it.
However, real world business applications would require to scale up much higher and this is where an n-tier scalable architecture comes in handy. The n-tier architecture allows you to put different parts of the system on different pieces of hardware to be able to quickly scale up to meet business requirements. You can quite easily create a Silverlight 3 business application using the .NET RIA Services SDK from Microsoft.
To create an n-tier Silverlight 3 application, you need to have the .NET RIA Services SDK downloaded and installed alongside the Silverlight 3 SDK. Once this is done, open VS2008 or VWD2008 and create a new Silverlight project. When the dialog box to create a Web project comes up, you will see a new option saying 'Enable .NET RIA Services'. Turn this on to get the new features for scaling up your application.
Starting with .NET RIA Services
When the project comes in, it will look exactly like a normal Silverlight 3 project. Let's now create a service layer that will allow the Silverlight application in the browser to communicate with the server. First you create a new LINQ to SQL class that connects to the tables in the database you wish to work with. Here, I'm using the standard Northwind database and the Products, Suppliers & Categories table.
Compile the Web project once to build the DataContext for the LINQ and then add a new item of the type Domain Service Class. When you create the class, you will be presented with a configuration dialog. Make sure you enable client access for all the entities. Also check the add, edit and delete features turn on for each entity. Finally, also turn on the metadata generation option (we'll use this a little later). This adds a new class (and a metadata class) that exposes the entities.
Open up the domain service class that you just created. You will see that the class has four methods for each entity that you have selected in the previous step ? Get, Insert, Update and Delete. The last 3 are available only if you choose to enable data editing in the dialog box. Here, we'll go ahead and use these methods itself. However, in most real world cases, you would want to modify or add new methods for different scenarios. We will do a minor modification to the GetProducts to see the effect. Simply change the line to read:
return this.Context.Products.OrderBy(e => e.ProductName);
 |
 |
|
Turn on the option ( as shown above) to enable RIA Services when creating a Silverlight project.
|
When creating a DomainService class you must enable the entities, their editing and metadata generation.
|
This will ensure that the list of products is always returned sorted by the name. Now go to the Silverlight project and open the MainPage.xaml file. Drop in a DataGrid control on the page and configure it like this: <data:DataGrid x:Name="dgData"></data:DataGrid>.