NET RIA Services is a set of tools and libraries that make the life of a RIA (Rich Internet Application) developer much easier. Basically if you are writing a Silverlight application that will ever need to talk to your server, then .NET RIA Services will prove to be very useful.
The beauty of this programming model is that you get to work with high level entities, instead of low level relational database constructs. If you don’t know why entities are interesting, see a previous blog post:
These entities are easily queried using LINQ. In addition, your typical business application will need to be able to create, update, and delete data. All these abilities are built into RIA Services.
Other great abilities include the ability to maintain local state in Silverlight, manage dirty entities, perform batch updates, perform transactions, and even access authentication, authorization, and profiles.
All these great things happen with http, whose often difficult programming model, has been abstracted away.
My previous blog post built the project you see below. Make sure you quickly go through that post so the rest of this makes sense to you.
The Northwind database – where to download
Do a search on Bing for “Northwind and pubs Sample Databases for SQL Server 2000” and you should find this link:
The Northwind database – building the database
The key file is instnwnd.sql.
This is only a fraction of the .sql code.
Start Microsoft SQL Server Management Studio and choose, “File, Open, File”
Open instnwnd.sql and go to SQL Server Management Studio and hit the “f5” key or go to the menu and choose “Query, Execute.”
Notice that we have a Northwind database. We will work with some of the tables here in our Silverlight RIA Services project.
The Northwind database – adding and ADO.NET Entity Model
We need to add a third project. It will be used to contain our data model. In reality, we will use it to contain an ADO.NET Entity Model.
Add a “New Item” to our “HelloWorld.Web” project.
- This will hold our ADO.NET Entity Model that Silverlight RIA Services will consume.
- The ADO.NET Entity Model has a lot of useful functionality and will help as a layer of abstraction for our Silverlight application
- See http://blogs.msdn.com/brunoterkaly/archive/2010/01/25/ado-net-entity-framework-and-net-4-how-to-use-visual-studio-2010-modeling-tools-to-build-a-database.aspx for some reasons why we like working with “entities” instead of relational data.
Silverlight will use an ADO.NET Entity Data Model to query the data store.
We will generate our entity model using the previously created Northwind database.
Select the Northwind database that your created in previous steps:
Make sure to select 3 tables:
- Order Details
The finished entity model. Our Silverlight RIA Services Application is ready to move forward. In a moment, we are going to add a “Domain Service.”
In the HelloWorld.Web project add a “New Item to the “Services” folder.
Perform the following:
- Choose Domain Service Class
- Give a name of NorthWindDomainService
Note that we checked a total of 8 checkboxes
- 3 to select the entities (Customer, Order, Order_Detail)
- 3 to enable editing (we want to do basic CRUD – Create, Read, Update, Delete)
- 1 to enable client access (exposing the data to client browsers)
- 1 to generate data for associated metadata (used in validation later)
What does the Domain Service do? It is the heart of a WCF RIA Services application. It is a beautiful thing because it manages serializing our objects for both the server side and client side. We only write our code once and both client and server projects in our solution have our data model available and easily programmable.
Think of it as the server side of a communication with a remote browser based client. If you look at the Silverlight project, you will note that some code has been generated for us. Let’s learn some of the deeper details.
The Domain Context is what the client works with. It is a perfect mirror of the Domain Service, which runs on the Server.
First rebuild your solution (this will generate code)
Next, click on “Show All Files”
By looking at the generated code you can learn more about what is taking place. Here is the interesting part. When you compile, the code gets copied to the Silverlight Project. In other words, your “Domain Service” becomes your “Domain Context” without having you write any code. When you compile, that is when the Domain Service “morphs” itself into the Domain Context on the client.
This is nothing more than traditional proxy code. You should not edit this code because it will be re-generated once you compile.
The code that is reflected is placed in the Domain Context.
Here is a list of classes that gets generated for client consumption. Remember the goal – to get a DomainContext, which is a stateful client side representation of a DomainService, providing access to all the functionality of the DomainService.
public sealed partial class WebContext : WebContextBase
– Provides a context to make services and other resources available for the application.
public sealed partial class AuthenticationContext : global::System.Windows.Ria.ApplicationServices.AuthenticationDomainContextBase
– A context used for authentication
public sealed partial class User : Entity, global::System.Security.Principal.IIdentity, global::System.Security.Principal.IPrincipal
– A user representing the request for data
public sealed partial class UserRegistrationContext : DomainContext
public sealed partial class NorthWindDomainContext : DomainContext
A DomainContext is a stateful client side representation of a DomainService, providing access to all the functionality of the DomainService.
internal sealed class AuthenticationContextEntityContainer : EntityContainer
internal sealed class UserRegistrationContextEntityContainer : EntityContainer
internal sealed class NorthWindDomainContextEntityContainer : EntityContainer
Represents a cache of entities in the form of a collection of EntitySets.
public sealed partial class Customer : Entity
public sealed partial class Order : Entity
public sealed partial class Order_Detail : Entity
public sealed partial class RegistrationData : Entity
Our entities being consumed by Silverlight runtime at the client browser
- When you add a method to a domain service, it gets automatically reflected back to the the Silverlight “HelloWorld” project.
- You need to recompile after adding a method to NorthWindDomainService.cs
You may wish to add custom methods to the domain service. For example, in the code below you can see that Intellisense is a big help.
Lets add a method that returns customers from the United Kingdom.
When I compile my project, this custom method in my Domain Service will get reflected back to the client Silverlight project.
You will add this custom method before re-compiling.
Let’s compile to make sure there are no mistakes.
Now confirm that the generated code made it over. Click “Show all files” display HelloWorld.Web.g.cs
Notice that we can retrieve our 3 entities:
- Order Details
Also note the we a great deal of useful boilerplate code.
As previously explained, views are for displaying data. Notice that “Entities” exist in the “Client” project.
Here are the steps when a user wants to see data in the browser:
- The client sends a query to the server
- The server execute the query, packages up the result in the form of an entity
- The server sends the entities (1 or more) back to the client
The client might also wish to allow the editing of data, in which case the client “submits” the data and receives a subsequent “results” object.
Adding a View
Right mouse click on the “Views” folder for the Silverlight project.
Add a “Silverlight Page” and call it “Customers.”
The next step is add a navigation link on the main page. The link will allow users to navigate to the page we just added, “Customers.xaml.”
- Open MainPage.xaml by right mouse clicking and choosing “View Designer.”
- Navigate to the <HyperlinkButton…>
We need to add a “Customers” hyperlink to MainPage.xaml. Notice the xaml code below at the lower arrow. He will just copy and paste “Home” and change the XAML to point to “Customers.xaml,” the page we just added.
MainPage.xaml now contains the new hyperlink. Notice lines 50 to 53. Notice the “NavigateUri” is pointing to “/Customers” which will map to Customers.xaml (line 51).
Note that MainPage.xaml now supports a hyperlink. Let’s run the project and see if it works as expected. Go to the “Build” menu and choose “Rebuild All.”
Let’s go back to the Customers.xaml and make some simple additions to say “hello.”
Change the label content to say “Customer Listing”
The XAML code for Customers.xaml now looks like this:
Build the solution, then run.
Click on the “Customers” menu selection and the correct result happens!
This post has been useful for several reasons because we learned:
- How work with databases and how to incorporate and ADO.NET Entity Model to our RIA Services application. However, we haven’t yet display customer data. That will happen in the next post.
- How the relationship works between the “Domain Service” on the server and the “Domain Context” on the client (Silverlight app) to supply “entities” to the browser application (Silverlight).
- How to add custom views to the Silverlight client. This a critical skill moving forward as we expose more and more data to the end user.