Data Access has been discontinued. Please refer to this page for more information.

Walkthrough: Creating a RIA Services Solution

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

In this walkthrough, you will create a WCF RIA Services application that retrieves data from the SofiaCarRental sample database. You will use Silverlight to create the client in the presentation tier. You will create entity classes that represent various database tables. This walkthrough serves as a starting point for other walkthroughs.

In this topic:

To set up a RIA Services solution:

  1. In Visual Studio, create a new RIA Services project by selecting File, New, and then Project. The New Project dialog box appears.
  2. Select the Silverlight Application template and name the new project OA.SL.RIA.Demo.

  3. Click OK. The New Silverlight Application dialog box appears.

  4. Select the Enable WCF RIA Services check box. By checking this check box, you create a RIA Services link between the client project and the server project.

  5. Click OK to create the solution. The solution contains two projects: a client project and a server project. The client project is named OA.SL.RIA.Demo and it contains the Silverlight code that you use to create the presentation tier. The server project is named OA.SL.RIA.Demo.Web and it contains the middle-tier code.

Creating Telerik Data Access Domain Model

In this section, you will create a Telerik Data Access Domain Model that represents data from the SofiaCarRental sample database. RIA Services works with a variety of data modeling classes and data sources.

To make the data available in the middle tier:

  1. In Solution Explorer, right-click the server project (OA.SL.RIA.Demo.Web), select Add, and then select New Item. The Add New Item dialog box appears.
  2. In the list of categories, select Data and then select the Telerik Data Access Domain Model item.
  3. Name the new file SofiaCarRentalDomainModel.rlinq and click Add. The Telerik Data Access Create Model Wizard appears.
  4. In the Select Domain Model Type screen, select Populate from database. Enter SofiaCarRentalDbContext for model name.
  5. In the Setup Database Connection screen, create a data connection to the SofiaCarRental database and click Next.

  6. In the Choose Database Items screen, select all the tables.

  7. Click Finish. Persistent classes are generated for the tables.

  8. Build the solution.

Creating a Domain Service

In this subsection, 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.

To create a domain service:

  1. Right-click the server project, select Add and New Item.
  2. In the list of categories, select Web and then select the Telerik Data Access Domain Service class template.
  3. Name the class SofiaCarRentalDomainService.tt
  4. Click Add. The RIA Wizard appears.

    There are three options. You can create domain service by using OpenAccessContext class, Rlinq File or Fluent Metadata Source. The first two options are equivalent. If you have "code-only" scenario, i.e. you are using the Fluent Mapping API, then you need to select the third option (Fluent Metadata Source). You could find more information about Telerik Data Access Fluent Mapping API here. For the purpose of this tutorial select Rlinq file and press Next.

  5. On the next step, you need to configure the Domain Service Endpoints.

  6. Set the Domain service name option to SofiaCarRentalDomainService. Make sure that the Enable client access is checked. In the Available metadata sources drop-down, select the SofiaCarRentalDomainModel.rlinq element. Check the Enable and Enable Edit settings for all entities in the grid control.

  7. Click Finish. The domain service class is generated.

  8. Open the SofiaCarRentalDomainService.generated.cs file.

    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class SofiaCarRentalDomainService : DomainService
    {
      // Generated code...
    }
    
    <EnableClientAccess()> _
    Public Partial Class SofiaCarRentalDomainService
     Inherits OpenAccessDomainService(Of SofiaCarRentalDbContext)
    End Class
    
  9. The file should have the following characteristics:

    1. The SofiaCarRentalDomainService class should derive from the OpenAccessDomainService<T> class which is an abstract base class in the Telerik.OpenAccess.Ria.Extensions.dll assembly.

      [EnableClientAccess()]
      public class SofiaCarRentalDomainService : OpenAccessDomainService<SofiaCarRentalDbContext>
      {
       // Generated code...
      }
      
      <EnableClientAccess()>
      Public Class SofiaCarRentalDomainService
      Inherits OpenAccessDomainService(Of SofiaCarRentalDbContext)
        ' Generated code...
      End Class
      
    2. The generic base class is bound to the OpenAccessContext class that was created in the previous steps (in our case this is SofiaCarRentaDbContext).

    3. The SofiaCarRentalDomainService class is marked with the EnableClientAccessAttribute attribute to indicate that it is visible to the client tier.
    4. Query methods named GetEntity (for example GetCustomers). This methods return every item without any filtering or sorting.
    5. Methods to insert, update, and delete records are generated. For example:

      [EnableClientAccess()]
      public class SofiaCarRentalDomainService : OpenAccessDomainService<SofiaCarRentalDbContext>
      {
      public IQueryable<Customer> GetCustomers()
      {
      return this.DataContext.Customers;
      }       
      public void DeleteCustomers(Customer customer)
      {
      // This is a callback method. The actual Delete is performed internally.
      }
      public void UpdateCustomers(Customer customer)
      {
      // This is a callback method. The actual Update is performed internally.
      }
      public void InsertCustomers(Customer customer)
      {
      // This is a callback method. The actual Insert is performed internally.
      }
      }
      
      <EnableClientAccess()>
      Public Class SofiaCarRentalDomainService
      Inherits OpenAccessDomainService(Of SofiaCarRentalDbContext )
      Public Function GetCustomers() As IQueryable(Of Customer)
       Return Me.DataContext.Customers
      End Function
      Public Sub DeleteCustomers(ByVal customer_Renamed As Customer)
       ' This is a callback method. The actual Delete is performed internally.
      End Sub
      Public Sub UpdateCustomers(ByVal customer_Renamed As Customer)
       ' This is a callback method. The actual Update is performed internally.
      End Sub
      Public Sub InsertCustomers(ByVal customer_Renamed As Customer)
       ' This is a callback method. The actual Insert is performed internally.
      End Sub
      End Class
      
  10. Build the solution to ensure that everything is building successfully.

Creating the Silverlight Client Application

For this walkthrough, you will use the generated GetCustomers method.

Because a RIA Services link exists between the client project and the server project, client proxy classes are generated when you build the solution. These proxy classes enable you to access the data from the client.

To see the generated client proxy classes:

  1. Build the solution. When you build the solution, code is generated in the client project.
  2. In Solution Explorer, click Show All Files for the client project. Notice that the Generated_Code folder contains a code file.

  3. Open the code file in the Generated_Code folder. Notice that the file has the following characteristics:

    1. A WebContext class that derives from the WebContextBase class is generated.
    2. A SofiaCarRentalDomainContext 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.
    3. 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.

To display the data in the Silverlight client:

  1. Open MainPage.xaml.
  2. From the Toolbox, drag a DataGrid control to the Grid element in the XAML view. An XML namespace and a reference to a Data assembly are automatically added when you drag the DataGrid from the Toolbox. If you add the DataGrid without dragging it from the Toolbox, you must add a reference to the System.Windows.Controls.Data assembly and an XML namespace for xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data".
  3. Name the DataGrid CustomersDataGrid as it is shown in the following XAML.

    <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
                x:Class="OA.SL.RIA.Demo.MainPage"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
       <Grid x:Name="LayoutRoot"
             Background="White">
           <sdk:DataGrid x:Name="CustomersDataGrid"/>
       </Grid>
    </UserControl>
    
  4. Open the code-behind for MainPage.xaml.

  5. Add using statements for the OA.SL.RIA.Demo.Web namespace and the System.ServiceModel.DomainServices.Client namespace. The OA.SL.RIA.Demo.Web namespace is in the generated code file for the client project.
  6. Add code to instantiate the SofiaCarRentalDomainContext, retrieve customers by calling the GetCustomersQuery method, and bind the results to the DataGrid, as shown in the following code.

    using System.ServiceModel.DomainServices.Client;
    using System.Windows.Controls;
    using OA.SL.RIA.Demo.Web;
    namespace OA.SL.RIA.Demo
    {
       public partial class MainPage : UserControl
       {
           private SofiaCarRentalDomainContext domainContext = new SofiaCarRentalDomainContext();
           public MainPage()
           {
               InitializeComponent();
               LoadOperation<Customer> loadOperation = domainContext.Load<Customer>( domainContext.GetCustomersQuery() );
               CustomersDataGrid.ItemsSource = loadOperation.Entities;
           }
       }
    }
    
    Imports System.ServiceModel.DomainServices.Client
    Imports OA.SL.RIA.Demo.Web
    Imports OA.SL.RIA.Demo.Web.OA.SL.RIA.Demo.Web
    Partial Public Class MainPage
        Inherits UserControl
        Private domainContext As New SofiaCarRentalDomainContext()
        Public Sub New()
            InitializeComponent()
            Dim loadOperation_Renamed As LoadOperation(Of Customer) = domainContext.Load(Of Customer)(domainContext.GetCustomersQuery())
            CustomersDataGrid.ItemsSource = loadOperation_Renamed.Entities
        End Sub
    End Class
    
  7. Run the application. You should see a data grid that is similar to the following.

Next Steps

This walkthrough demonstrates only the basic steps to create a project and to retrieve some data from a domain service. To learn more about some additional capabilities, check out the next walkthrough.