(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

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

(10 votes)
Pencho Popadiyn
>
Pencho Popadiyn
Joined Apr 30, 2008
Articles:   22
Comments:   97
More Articles
7 comments   /   posted on Jun 23, 2010
Categories:   Data Access , Line-of-Business , General

This article is compatible with the latest version of Silverlight.

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?

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 Visual Studio
  • Silverlight Tools for Visual Studio
  • 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.


Subscribe

Comments

  • -_-

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


    posted by Bernhard on Oct 27, 2010 19:20

    Hi Pencho,

    you have written a very good and helpful tutorial. Thank you very much!

    Now I have a little problem, perhaps somebody can give me a tip:
    When I press F5 I get this warning message:

    "The Silverlight project you are about to debug uses WCF RIA services. WCF RIA services will fail unless teh Silverlight project is hosted in and launched from the same web project that contains the WCF RIA services."

    What can I do? What's wrong?

  • -_-

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


    posted by Bernhard on Oct 28, 2010 12:22

    Hi,

    I have solved the problem (see previous post). The only thing I had to do was to mark the server project (MyOpenAccessRioDemo.Web) as StartUp Project in Visual Studio. ... And additionally I had to reboot my computer

    Sorry for the inconvenience.
    Bernhard

  • ppopadiyn

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


    posted by ppopadiyn on Oct 28, 2010 12:24
    Great news Bernhard, happy to hear that
  • -_-

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


    posted by Patrick on Mar 05, 2011 23:00
    Can we have a VB.NET example?
  • ppopadiyn

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


    posted by ppopadiyn on Mar 05, 2011 23:31

    Hello Patrick,

    You may take a look at the official OpenAccess Help. There are VB.NET code snippets as well.

    http://www.telerik.com/help/openaccess-orm/getting-started-root-quickstart-sl-wcfria-overview.html

  • -_-

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


    posted by Wolfgang on Mar 21, 2011 05:34
    Great tutorial to get started! Would be interesting to see a follow up applying a MVVM on the client side instead of using code behind.
  • ppopadiyn

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


    posted by ppopadiyn on Mar 21, 2011 10:36

    Hello Wolfgang,

    Thank you about your interest in Telerik OpenAccess ORM. The purpose of this topic is to illustrate you how to integrate Telerik OpenAccess ORM in your Silverlight application. Using Mvvm is common, nevertheless you use OpenAccess or Entity Framework. I would recommend you this topic for more info: http://www.silverlightshow.net/items/WCF-RIA-Services-Part-4-Integrating-with-the-Model-View-ViewModel-Pattern.aspx

Add Comment

Login to comment:
  *      *       

From this series