Recommended

Skip Navigation LinksHome / Articles / View Article

WCF RIA Applications with Telerik OpenAccess ORM Part I: The Basics

+ Add to SilverlightShow Favorites
0 comments   /   posted by Pencho Popadiyn on Jun 23, 2010
(9 votes)
Categories: Tutorials , QuickStarts

1. 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

2. Introducing Telerik OpenAccess ORM

Telerik OpenAccess is an Enterprise-grade .Net ORM that does the data access plumbing in desktop and web applications. Nearly every application deals with data that needs to live longer than the actual execution of the program. This is also true for .NET applications. There are various ways to store this application data, many of which involve parsing the data to and from disk files. Although this is an easy way to make data persistent, realistically it can be only used for simple object graphs. Furthermore, essential features such as transactions, data protection and query capabilities are missing. Telerik OpenAccess ORM is a powerful object-relational mapper providing transparent persistence that fully conforms to the .NET standard. With Telerik OpenAccess ORM, you can easily store your .NET application objects directly in the underlying database store. Telerik OpenAccess ORM provides developers with a smart data access layer, which provides persistence services for effectively building well designed object-oriented applications.

Telerik OpenAccess ORM provides a full control of the mapping process and the generated model with the powerful Visual Designer and the advanced features for code optimization and caching.

 

3. Why should I choose Telerik OpenAccess ORM?

Ok, that question is fair enough. While the previous section was something like a “marketing introduction”, everything that I'm going to say here is my personal opinion. At first position I will place the great support. No matter what your problem is; what your question is; if you have a suggestion; or you are unhappy with the product, the OA team will do its best to help you. I have the opportunity to communicate every day with the OpenAccess team and to work tightly with them, and can ensure you that the guys out there are absolute professionals. It is so easy and just like a dream for every developer to work with them. Last but not least, they are always in pace with the new technologies, constantly improving the product and releasing new tools.

4. But is it free?

The answer is “yea and nay”. Like most of the enterprise products, Telerik OpenAccess ORM is not free. Of course you could still use a trial version of the product. However, the OA guys offer you an express edition – this is Telerik OpenAccess ORM Express. The only limitation of the Express version is the sole support of free database (for example SQL Server Express edition). So if you want to evaluate the product you could download the Express edition for free.

5. 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

6. Creating a RIA Service Solution

As I mentioned in the introduction, 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.

7. Creating 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’ve 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.

8. Creating DomainService

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 DomainService 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 DomainService class.

  • Open the NorthwindDomainService.cs file.

    // TODO: Create methods containing your application logic.
    [EnableClientAccess()]
    public class NorthwindDomainService : OpenAccessDomainService<NorthwindDbContext>
    {
    }
     
  • The file should has 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.

9. 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"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
        <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:

10. What’s next?

In the next article, I’ll show you how to add query methods in the DomainService, how to perform Create, Update, and Delete operations, and how to validate data. So stay tuned and meanwhile if you have any questions do not hesitate to contact me.

Share


Comments

Comments RSS RSS
No comments

Add Comment

 
 

   
  
  
   
Please add 2 and 5 and type the answer here:

Help us make SilverlightShow even better. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)