Friday, August 28, 2009

.NET RIA Service

.Net RIA Services bring together the ASP.NET and Silverlight platforms. It provides a pattern to write application logic that runs on the mid-tier. It integrates with Silverlight components on the client and ASP.NET on the mid-tier. Get more information about RIA services from http://www.nikhilk.net/NET-RIA-Services-Vision-Architecture.aspx.
In this blog we see how to create a simple .NET RIA Service enabled project. It only provides a brief overview of.Net RIA Services.
Step 1: First Create a new Silverlight Project as:pic1
Step 2: Then enable .Net Ria Services like this:
clip_image004[1]
Step 3 : Now to mid-tier i.e., on RiaTest.Web project add a new item and select the Data category as follows:
clip_image006[1]
we will access the Northwind database using the Entity Framework. We choose 2 tables from the northwind for this application.
clip_image008[1]
Step 4 : After building project create a DomainService on the mid-tier. A DomainService is a class that exposes entities and operations for a specific data domain. It is also where the developer adds application logic. To create a Domain Service Add new item to the mid-tier and in the Web category add DomainService class as follows:
clip_image010
Step 5 : Now we select only one table Orders , we will not set Enable Editing for the table because we want only read only table. Generate metada will create metadata of table on client.
clip_image012
It will generate a class as follows:
clip_image014
It has following characteristics:
· It is derived from LinqToEntitiesDomainService, an abstract base class built into the .NET RIA service framework3.
· It is bound to NorthwindEntities1 class we created earlier
· A single GetOrders() query method is generated because we select only Orders table
· This.Context.Orders in GetOrders() will give all the order in table.
· OrderService is marked with [EnableClientAccess] indicating it is visble on client tier
When we build project and click on Show all files on client tier we see some Gnerated code as:
clip_image016
This is the client proxy class which is generated from the Domain service class which we create on mid-tier.
Step 6 : Now in MainPage.xaml add a data Grid named “MyGrid” as follows:
image
Don’t forget to include System.Controls.Data.dll in references.
Step 7 : Now in MainPage.xaml.cs add 2 references :
· System.Windows.Ria.Data
· RiaTest.Web
Now create an object of OrderContext and then load entities of Orders and set the itemsource of the datagrid.
image
When you run the project you see all the orders in the data grid.