Telerik blogs

Introduction

In this short series of articles I’ll show you how to integrate Telerik OpenAccess ORM in Silverlight applications, how to solve issues when applying this pattern and how to take advantage from this great product. What will be covered?

  • WCF RIA Applications with Telerik OpenAccess ORM Part I: The Basics
  • WCF RIA Applications with Telerik OpenAccess ORM Part II: Create, Update, Delete and Validation
  • Creating Silverlight Applications with Telerik OpenAccess Data Service Wizard
  • WCF Data Services with Telerik OpenAccess ORM
  • WCF End Points Services with Telerik OpenAccess ORM
  • Silverlight Applications for Windows Azure with Telerik OpenAccess ORM Part I: Connection to SQL Azure
  • Silverlight Applications for Windows Azure with Telerik OpenAccess ORM Part II: Developing the Silverlight Client and Deploying the Application
Prerequisites

In order to complete the walkthroughs, the following components should be installed:

  • Telerik OpenAccess ORM or Telerik OpenAccess ORM Express Edition. You could download it from here. Remember that the only difference between the Express Edition and the full product is that OpenAccess Express supports only free databases.
  • Microsoft VisualStudio 2010
  • Silverlight 4 Tools for Visual Studio 2010
  • Northwind database

Creating the RIA Service Solution

In the first part of the series, I’ll cover how to create a WCF RIA Service by using Telerik OpenAccess ORM and how to consume that service from a Silverlight Client application.

So open your Visual Studio and follow me. Create a new Silverlight project and name it, let’s say MyOpenAccessRiaDemo. Of course do not forget to check the Enable WCF RIA Services check box. By checking this check box, you will create a RIA Services link between the client project and the server project. Nothing unusual so far. Your solution should contain two projects: a client and a server project. The client project is named MyOpenAccessRiaDemo and it will contain the Silverlight code. The server project is named MyOpenAccessRiaDemo.Web and it will contain the middle-tier code.


Creating a Telerik OpenAccess Domain Model

The next step is to create a Telerik OpenAccess Domain Model that represents data from the Northwind sample database. RIA Services work with a variety of data modeling classes and data source. For example, you can use either Entity Framework or OpenAccess. If you are familiar with Entity Framework, then you should not have difficulties with the creation of Telerik OpenAccess Domain Model, but to make the data available in the middle tier.

  • In the Solution Explorer, right-click the server project (MyOpenAccessRiaDemo.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 Telerik OpenAccess Domain Model item. It will be available only if you have installed Telerik OpenAccess ORM.


  • Name the new file NorthwindDomainModel.rlinq and click Add. The OpenAccess Data Wizard appears.
  • In the Choose Data Connection screen, create a new connection to the Northwind database and click Next.


  • In the Choose Database Objects screen, select all tables.
  • In the same dialog, name the domain model NorthwindDbContext


  • Click Finish to generate the model. Persistent classes are generated for all tables from the Northwind database.


  • If you want – double click the .rlinq file to open it in the Visual Designer, and also to get an idea about the Visual Designer and its capabilities.
  • Build the Solution.

Creating the Domain Service

The next several steps are extremely important. Why? The answer is that the creation of a Domain Service with Telerik OpenAccess ORM requires some additional work in comparison with the cases when you use Entity Framework on the server side. 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 in order to manage how the client interacts with the data. To create a domain service:

  • Right-click the server project and select Add Reference.
  • The Add Reference dialog appears. Go to the Browse tab. Navigate to the Telerik OpenAccess ORM installation directory. Go to the bin folder and select Telerik.OpenAccess.RIA.Extensions.dll and click Ok.


  • OpenAccess provides a base class named OpenAccessDomainService for domain services for persistent instances provided by an OpenAccessContext. The OpenAccessDomainServices derives from DomainService<T>.
  • Right-click the server project and select Add Reference. Add reference to the following assemblies:
    • System.ServiceModel.DomainServices.Server.dll
    • System.ComponentModel.DataAnnotations.dll
    • System.ServiceModel.DomainServices.Hosting.dll
  • 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 NorthwindDomainService.cs
  • Click Add. The Add New Domain Service Class dialog box appears.


  • Here is the first tricky moment. The Add New Domain Service Class dialog recognizes only DataContexts based on Entity Framework or Linq2Sql. Our domain model won’t be shown in the list with the available data sources. So leave the dialog as it is and click OK to generate the Domain Service class.


  • Open the NorthwindDomainService.cs file.
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class NorthwindDomainService : OpenAccessDomainService<NorthwindDbContext>
{
}

 

  • The file should have the following characteristics (you should add them manually):
    • The NorthwindDomainService class should derive from OpenAccessDomainService<T> class which is an abstract base class in the Telerik.OpenAccess.Ria.Extensions assembly.
    • The generic base class is bound to the OpenAccessContext class that was created in the previous steps (in our case this is NorthwindDbContext).
    • The NorthwindDomainService class is marked with the EnableClientAccessAttribute attribute to indicate that it is visible to the client tier.
    • A query method named GetCustomers. This method returns every item without any filtering or sorting
// TODO: Create methods containing your application logic.
[EnableClientAccess()]
public class NorthwindDomainService : OpenAccessDomainService<NorthwindDbContext>
{
    public IQueryable<Customer> GetCustomers()
    {
        return this.DataContext.Customers;
    }
}
  • Build the solution to ensure that everything builds successfully.

Creating the Silverlight Client Application

In this section, I’ll show you how to use the generated domain service and how to consume data. But first, let’s take a look at some of the characteristics of the project. 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 class:

  • Build the solution. When you build the solution, code is generated in the client project.
  • In the Solution Explorer, click Show All Files for the client project. Notice that the Generated_Code folder contains a code file. Open it, you'll see that the file has the following characteristics: A WebContext class that derives from the WebContextBase class is generated. A NorthwindDomainContext class that derives from the DomainContext class is generated. This class has a method 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.

To display data in the Silverlight client:

  • Open MainPage.xaml.
  • From the Toolbox, drag a DataGrid control within the LayoutRoot element in the XAML view.
  • Name the DataGrid CustomersGrid as it is shown in the following XAML:
<UserControl  x:Class="MyOpenAccessRiaDemo.MainPage"
    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid Name="CustomersGrid" />
    </Grid>
</UserControl>
  • Switch to the code-behind.
  • Add using statements for the MyOpenAccessRiaDemo.Web and System.ServiceModel.DomainServices.Client namespaces. The MyOpenAccessRiaDemo.Web is in the generated code file for the client project.
  • Add code to instantiate the NorthwindDomainContext, retrieve customers by calling the GetCustomersQuery method, and bind the results to the DataGrid, as demonstrated in the following code.
using System.ServiceModel.DomainServices.Client;
using System.Windows.Controls;
using MyOpenAccessRiaDemo.Web;
  
namespace MyOpenAccessRiaDemo
{
    public partial class MainPage : UserControl
    {
        private NorthwindDomainContext domainContext = new NorthwindDomainContext();
        public MainPage()
        {
            InitializeComponent();
            LoadOperation<Customer> loadOperation =
                domainContext.Load<Customer>( domainContext.GetCustomersQuery() );
            this.CustomersGrid.ItemsSource = loadOperation.Entities;
        }
    }
}
  • Run the application. You should see a data grid that is similar to the following:


 

What’s next?

In the next article, I’ll show you how to add query methods in the Domain Service, how to perform Create, Update, and Delete operations, and how to validate data. Stay tuned for the next blog.

 


 

 


Comments

Comments are disabled in preview mode.