Published: 28 Jul 2009
By: Faisal Khan

What RIA services actually are and how to use them with Silverlight.

Contents [hide]

What is RIA Services

With the invention of Silverlight 3 Microsoft introduces the RIA Services which have given a new dimension to the traditional n-tier application pattern. With this pattern you can write application logic that runs on the mid-tier and controls access to data for queries, changes and custom operations. You can also provide end to end support for common tasks such as data validation, authentication and roles by integrating with Silverlight components on the client and ASP.NET on the mid-tier. There’s a great walkthrough video on RIA Services by Tim Heuer. This article is based on Tim’s walkthrough with just a little bit of improvisation. Let’s delve into a practical demonstration of RIA Services.

Starting the Project

I assume you have already installed the RIA Services July Preview. Open Visual Studio and create a new project.

Figure 1: Creating New Project in Visual Studio

Creating New Project in Visual Studio

This will bring up the new project template dialog box and if you already installed RIA services, you’ll be able to see the template SilverlightBusineesApplication.

Figure 2: Project Templates in Visual Studio

Project Templates in Visual Studio

Select SilverlightBusinessApplication and name the application anything you like. In this case I’m going with the default name BusinessApplication1. Selecting this template will bring up the navigation UI template. You’ll be able to see this in design view.

Figure 3: Application UI in Design View of Blend

Application UI in Design View of Blend

By default we’ve added two applications: a Silverlight application and an ASP.NET web application using .NET RIA Services, though we don’t have any RIA Services yet. Let’s create one. We’ll have create, read, update, delete support with authentication in this application. Add a new ADO.NET Entity Data Model to the web application by right clicking over the Web application project template and select Add > New Item. This will bring up the following dialog box:

Figure 4: Adding ADO.NET Entity Data Model

Adding ADO.NET Entity Data Model

We are going to use the Northwind database for data. Name the model NwdModel. Clicking over the Add button will bring up the Entity Data Model Wizard.

Figure 5: Entity Data Model Wizard

Entity Data Model Wizard

Select Generate from database and then click the Next Button. You’ll see the following screenshot.

Now set up your connection and name the Entities. For this application I have named it NorthwindEntities. Click over the Next button. It will retrieve all the database information and after this you’ll see a screenshot similar to the one below.

Now choose a table. For this sample I’ve choosen the employees table.

Click Finish. Now we have our Model. Build the application by pressing F6 so that we have an EntityModel we can build from. Now add a service to work with the data. If you look at the template of the web application you’ll see there’s a folder named services. Inside this folder there’s some authentication services automatically added. Add another one by right clicking over the Services folder and choosing Add>New Item. In the Add New Item dialog box choose Domain Service Class. This is a template that gets installed with RIA Services.

Figure 9: Adding Domain Services Class

Adding Domain Services Class

Name it NwdDomainService and click Add. A new window will pop up in front of you.

Check the Employees CheckBox, the Enable Editing CheckBox and the “Generate associated classes for metadata” CheckBox. That means we’ll work with Employees entity and we want to edit it. We also want to generate the associated classes for metadata. Click on the OK button. Now you should see server queries like GetEmployees(), InsertEmployees(), UpdateEmployees(), DeleteEmployees() etc. Like the following:

You can add any logic inside the above methods. You can also put a different type of queries like this:

Then we can have employees by HireDate. We can define all the queries here and the context within them.

Now we have our Domain Service Class. Remember that this is the code that will be projected on to our Silverlight application. When we build our project it will compile our code on server side and also generate some code on client side. Now we have a model, and a service that references our model.

Adding Views

In the Silverlight application project right click the Views folder and select Add New Item. Select Silverlight Page and name the page EmployeeListing. This will open a Silverlight navigation page. Change the title of the page to Employees. Here’s the XAML:

This is a navigation page and it’s going to be fit with our navigation framework. We need to add a link of this page to our MainPage. Open MainPage.xaml. You’ll be able to see the following XAML:

There are two HyperlinkButtons inside the StackPanel named LinksStackPanel. One button is for the link to Home and the other for the link to the About page. There’s also a Rectangle which acts as a divider. We need to place a link to EmployeeListing between these two pages. So add the following XAML inside LinksStackPanel:

Right click over the EmployeeListing.Xaml page and select open in ExpressionBlend. If you already installed Expression Blend 3 with Sketch Flow support, you’ll be able to open this page in Expression Blend. In Blend select LayoutRoot and change the Background of the LinearGradientBrush. Choose any color you like. In this case the value for the GradientStops of the LinearGradientBrush is something like the following:

Add a row to the LayoutRoot and add a TextBlock. Here’s the XAML:

Now if you run the application and navigate to the EmployeeList page you should see something like the following screenshot:

Now switch to Visual Studio and add a reference to System.Windows.Controls.Data by right clicking over the References in you Silverlight application project.

Go to the XAML view of EmployeeListing page and add this namespace:

Now add a DataGrid in the second row of LayoutRoot of EmployeeListing page. This will provide us with access to the DataGrid to add it in our page.

To populate our DataGrid we will use those services. Services are on our server side project and they have been projected on our Silverlight application. We need to add a reference to the domain service we created. Add a Loaded event for the EmployeeList and swith to the code behind by right clicking over the event name:

This will add a Loaded event in the code behind. Add this namespace in the EmployeeListing.Xaml.cs file:

Domain services get projected as something called DomainContext. Create a new instance of NwdDomainContext by placing the following code inside the Loaded event of EmployeeListing:

Now to populate the DataGrid assign the empDataGrid’s ItemsSource property to the employees entities from our model or from our Domain Service Class.

Then we can load those employees by query with code similar to the following:

This is the query or function we created in the NwdDomainService class. Run the application to see the populated Grid. You should see something like the following:

We didn’t add any service refernce or we didn’t have to do any asynchronous completed event or something like that. These three lines of code are just doing the trick for us.

First we created a new instance of NwdDomainContext. Then we bound it to the Employees entity and loaded the emDataGrid using the query GetEmployeesByHireDateQuery().

That’s it for now. In the next part we’ll see how to do this declaratively using XAML. I hope you’ve enjoyed this part of RIA Services with Silverlight 3.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Faisal Khan

Currently working as a freelance Silverlight/WPF UX Integrator. His passions revolve around creating the next generation desktop application and Rich Internet applications. His goals are to create applications which will serve the user in a friendly and traditional way, but will keep pace with nex...

This author has published 11 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category

Air Space Issue in Web Browser Control in Silverlight
Air Space issue is a common issue in Web Browser control in Silverlight and WPF. To explain the issu...
Displaying Notification Messages in a Silverlight Dashboard Application
In this article we will see how we could display a notification message and further a list of notifi...
TextBox Row Filter in Telerik's RadGridView in Silverlight
If you have come across the JQGrid features, you might have seen the filter row available as the fir...
Widget Refresh Timer in MVVM in Silverlight
In this article we'll see how to refresh and disable widgets using the Model View View-Model pattern...
Develop a Flexible 2.5D Scene Editor Targeting Silverlight RPG Games - Part 2
In this article, I'm going to introduce to you how to construct such a 2.5D RPG game scene editor th...

You might also be interested in the following related blog posts

RadGridView for Silverlight and WCF RIA Services Part II: Basic Editing Support read more
Using WCF with SQL Azure and Telerik OpenAccess read more
The Underground at PDC read more
Idle Timeouts in RIA Services Authentication read more
View and print Reporting Services Reports from Silverlight. read more
Visual Studio 2010 Extension Manager read more
Silverlight Live Streaming service update read more
Building A Product For Real read more
Silverlight MVP read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 26: Authentication and Personalization read more


Subject Author Date
placeholder RIA Services With Silverlight 3 - Part 1 David Kelley 7/28/2009 5:58 PM
.Net RIA Services Robert Hellestrae 7/30/2009 8:23 AM
placeholder Error after adding the domain service Welcome 2u 7/31/2009 2:44 AM
Error after adding the domain service Faisal Khan 7/31/2009 4:12 PM
placeholder RE: Error after adding the domain service Welcome 2u 8/1/2009 2:47 AM
Thanks so much Harry Cruise 8/13/2009 2:59 AM
placeholder RE: Thanks so much Faisal Khan 8/13/2009 6:05 AM
RE: RE: Thanks so much Faisal Khan 8/15/2009 8:09 PM
placeholder ria services out of browser Sunil Sharma 11/12/2009 7:44 AM
Refreshing John Hicks 12/5/2009 6:14 PM
placeholder RE: Refreshing Faisal Khan 12/5/2009 7:26 PM

Please login to rate or to leave a comment.