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: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
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.
Do a search on Bing for “Northwind and pubs Sample Databases for SQL Server 2000” and you should find this link:
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”
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.
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:
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:
Note that we checked a total of 8 checkboxes
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.
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 authenticationpublic sealed partial class User : Entity, global::System.Security.Principal.IIdentity, global::System.Security.Principal.IPrincipal - A user representing the request for data
**Key part**public sealed partial class UserRegistrationContext : DomainContextpublic 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 : EntityContainerinternal sealed class UserRegistrationContextEntityContainer : EntityContainerinternal sealed class NorthWindDomainContextEntityContainer : EntityContainer Represents a cache of entities in the form of a collection of EntitySets.
public sealed partial class Customer : Entitypublic sealed partial class Order : Entitypublic sealed partial class Order_Detail : Entitypublic sealed partial class RegistrationData : Entity Our entities being consumed by Silverlight runtime at the client browser
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
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 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.”
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:
This and the previous post were very thorough and helpful to Silverlight newcomers. Are there more that get into CRUD operations?
Can you please explain How Add, Update and Delete from domainservice are mapping in Auto Generate code.
Hi Bruno and thanks for your wonderful, well-explained post.
I have in my database a User, Role and UserRole table, I want to use my custom tables, not the aspnetdb's, how do I do it?