Walkthrough: Creating a Open Ria Services Solution
Last updated
Last updated
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
In this walkthrough, you create a Open Ria Services application that retrieves data from the AdventureWorksLT database and then makes it available to a Silverlight client where it is presented. You access the data from the data source by creating entity classes that represent various database tables on the server in the middle tier and then you present it by creating a domain service that makes these middle tier entities accessible to the Silverlight client. This walkthrough serves as the starting point for many of the other walkthroughs in the Open Ria Services documentation.
This and the other walkthroughs presented in the Open Ria Services documentation require several prerequisite programs, such as Visual Studio and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to Open Ria Services and the Open Ria Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for Open Ria Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this Open Ria Services walkthroughs.
Create a new Open Ria Services project in Visual Studio by selecting File, New, and then Project.
The New Project dialog box appears.
Select the Silverlight Application template from Silverlight group of the Installed Templates and name the new project RIAServicesExample.
Click OK.
The New Silverlight Application dialog box appears.
Select the Enable Open Ria Services check box near the bottom of the dialog box. Checking this box creates a Open Ria Services link between the client project and the server project.
Click OK to create the solution.
The solution contains two projects: a client project and a server project. The client project is named RIAServicesExample and it contains the Silverlight code that you use to create the presentation tier. The server project is named RIAServicesExample.Web and it contains the middle-tier code.
In this section, you will create the ADO.NET Entity classes that represent data from the AdventureWorksLT database. Open Ria Services works with a variety of data modeling classes and data sources. For more information on the options for accessing data with Open Ria Services, see the Data topic.
Warning: When using an Entity Data Model (EMD) with Visual Studio, you must select the Include foreign key columns in the model option. By default, this option is selected when you use the Entity Data Model wizard. You also must use the default behavior of embedding EMD mapping information in the assembly.
In Solution Explorer, right-click the server project, RIAServicesExample.Web, select Add, and then select New Item.
The Add New Item dialog box appears.
In the list of categories, select Data and then select the ADO.NET Entity Data Model template.
Name the new file AdventureWorksModel.edmx and click Add.
The Entity Data Model Wizard appears.
In the Choose Model Contents screen, select Generate from database option and click Next.
In the Choose Your Data Connection screen, create a data connection to the database and click Next.
In the Choose Your Database Objects screen, select the Address, Customer, and CustomerAddress tables.
Confirm that the Include foreign key columns in the model check box is checked by default and click Finish.
Entity models are created for the tables.
Build (with the key combination Ctrl+Shift+B) the solution.
In this section, you will add a domain service to the middle-tier project. A domain service exposes the data entities and operations in the server project to the client project. You can add business logic to the domain service to manage how the client interacts with the data.
Right-click the server project, select Add and New Item.
In the list of categories, select Web and then select the Domain Service Class template.
Name the class CustomerDomainService.cs (or CustomerDomainService.vb).
Click Add.
The Add New Domain Service Class dialog box appears.
Make sure that the Enable client access box is checked.
Select the Customer entity and then check the Enable Editing box for it.
Click OK.
The CustomerDomainService class is generated in a new CustomerDomainService.cs (or CustomerDomainService.vb) file.
Open this file. Notice that the file has the following characteristics:
The CustomerDomainService class derives from LinqToEntitiesDomainService\ class, which is an abstract base class in the Open Ria Services framework. This base class was used automatically because the domain service exposes an ADO.NET Entity data class.
The generic base class is bound to the entity class that was created in previous steps by the AdventureWorksLTEntities of type ObjectContext in its generic parameter.
The CustomerDomainService class is marked with the EnableClientAccessAttribute attribute to indicate that it is visible to the client tier.
A query method named GetCustomers is generated. This method returns every item without any filtering or sorting.
Methods to insert, update, and delete customers from the records have been generated.
In other walkthroughs, you will be shown how to add business logic to the domain service. For this walkthrough, you will simply use the GetCustomers method generated by default.
Client proxy classes are generated when you built the solution. The Open Ria Services link that was established between the client project and the server project make this code generation possible. These client proxy classes provide access to the data from the client.
Build the solution.
When you build the solution, code is generated in the client project.
In Solution Explorer, select the RIAServicesExample client project and click the Show All Files icon at the top of the window.
Notice the Generated_Code folder contains a RIAServicesExample.Web.g.cs (or RIAServicesExample.Web.g.vb) file.
Open the code file in the Generated_Code folder.
Notice that the file has the following characteristics:
A WebContext class that derives from the WebContextBase class is generated.
A CustomerDomainContext class that derives from the DomainContext class is generated. This class has a method named GetCustomersQuery that corresponds to the query method created in the domain service.
A Customer class that derives from the Entity class is generated for the entity exposed by the domain service. The Customer entity class in the client project matches the Customer entity on the server.
Open MainPage.xaml.
From the Toolbox on the left, drag a DataGrid control to within the Grid element in XAML view.
Dragging the DataGrid control from the Toolbox causes a namespace using System.Windows.Controls statement and a reference to a System.Windows.Controls.Data assembly to be added automatically.
[!WARNING] If you add the DataGrid without dragging it from the Toolbox, you must add the reference to the System.Windows.Controls.Data assembly to the client project and the using statement manually in the code behind file.
Change the value of the AutoGeneratedColums to True, name the DataGrid element CustomerGrid, and adjust the height and width attributes as shown in the following XAML.
Open the code-behind for MainPage.xaml.
Add using (C#) or Imports (Visual Basic) two statements: using RIAServicesExample.Web; and the using System.ServiceModel.DomainServices.Client;.
The RIAServicesExample.Web namespace is the namespace containing the generated code for the client project in the RIAServicesExample.Web.g.cs (or RIAServicesExample.Web.g.vb).
To instantiate the CustomerDomainContext, add the line of code private CustomerDomainContext _customerContext = new CustomerDomainContext(); in the MainPage class.
Retrieve customer entities by calling the GetCustomersQuery method with LoadOperation\: LoadOperation\ loadOp = this._customerContext.Load(this._customerContext.GetCustomersQuery());.
Bind the entities loaded to the DataGrid with CustomerGrid.ItemsSource = loadOp.Entities;.
To summarize, the MainPage.xaml.cs file should now contain the following code:
Run (F5) the application.
You should see a data grid that is similar to the following.
This walkthrough has shown only the basic steps to create a project and retrieve unfiltered data from a domain service. Here are some suggestions for learning about additional capabilities:
Create customized query methods, such as queries that accept a parameter, which are typically used for filtering data. For more information, see Walkthrough: Adding Query Methods.
Add business logic to a domain service that contains update, insert, and delete methods and that manages the process for modifying the data. For more information, see How to: Add Business Logic to the Domain Service.