Telerik blogs

With the Q1 release of Telerik OpenAccess ORM, Telerik released a brand new LINQ Implementation and supporting Visual Entity Designer. With the upcoming Q2 release next month, we will introduce full WCF RIA Services support. If you want to get started now you can wire up the services yourself pretty easily. Let’s take a look at how to get your feet wet with RIA Services and Telerik’s LINQ implementation.

Before you get started, you will need a few things installed:

  • Visual Studio 2010
  • Silverlight 4
  • WCF RIA Services for Visual Studio 2010
  • Northwind sample database
  • Telerik OpenAccess ORM Q1 Service Pack 1 or higher

Getting Started: The Easy Stuff

Let’s create a new Silverlight application first. In the New Silverlight Application dialog, check the “Enable WCF RIA Services” checkbox. This will enable RIA Services.

image

The next step is to create a new Telerik Domain Model in the server (ASP.NET) project. I have a detailed walk through here on how to do that. We’ll create a new Domain Model by right clicking on the server project and selecting “Add” and choosing the Telerik Domain Model from the menu. Then we will map all of the tables from Northwind using the wizard. We’ll also keep the default model name of NorthwindEntityDiagrams.

image

We’re in good shape. So far if you have used the new LINQ Implementation nothing is new (or LINQ to SQL/EF for that matter.)  Now let’s add the RIA Services stuff.

Housekeeping-Adding References

Since our RIA Services support is still beta, you have to wire up a few things manually, including some references. You need to add:

  • Telerik.OpenAccess.Ria.Extensions.dll (found under “Browse: Program Files|Telerik|OpenAccess ORM|Bin)
  • System.ServiceModel.DomainServices.Server.dll
  • System.ServiceModel.DomainServices.Hosting.dll
  • System.ComponentModel.DataAnnotations.dll

image

Now we are ready to create the domain class.

Creating the Domain Class

Add a new Domain Service Class by right clicking and selecting Add|New Item and choose Domain Service Class.

image

Accept the defaults in the dialog and then we are ready to go. (Note at this time OpenAccess does not support creation of the class for metadata, but will soon, possibly even before Q2.)

image

Once you accept this dialog, a new empty class is generated.

 1:  [EnableClientAccess()]
 2:  public class DomainService1
: DomainService
 3:  {
 4:  }

 

We need to add a using statement so we can make sure our DomainService uses the OpenAccess model: using Telerik.OpenAccess;

Now change the inheritance of DomainService1 to this:

 1: [EnableClientAccess()]
 2: public class DomainService1
: OpenAccessDomainService<NorthwindEntityDiagrams>
 3: {
 4: }

Now we have one last step to create our DomainService, we have to add the CRUD methods. (In the future all of this will be done automatically for you!)

 1: {
 2:  public IQueryable<Customer>
GetCustomers() 
 3:  { 
 4:  return this.DataContext.Customers; 
 5:  }
 6:  
 7:  public void InsertCustomer(Customer
c)
 8:  {
 9:  this.DataContext.Add(c);
 10:  this.DataContext.SaveChanges();
 11:  }
 12:  public void DeleteCustomer(Customer
c)
 13:  {
 14:  this.DataContext.Delete(c);
 15:  this.DataContext.SaveChanges();
 16:  }

These are the methods of your DomainService. You can also add business logic here. Let’s do that with our GetCustomers() query.  I will write some business logic that filters all of the customers by the country of Germany. Of course you would have more complex business logic here, however, I just want to demonstrate the point. All clients that use this DomainService will inherit this business logic, even if you expose your service as an OData feed. Our implementation is here:

 1: public IQueryable<Customer>
GetCustomers() 
 2: { 
 3:  return this.DataContext.Customers
 4:  .Where(c=> c.Country=="Germany")
 5:  .OrderBy(c=> c.CustomerID); 
 6: }

 

Now you are done. Compile and let’s get cracking on a Silverlight client.

Creating the Silverlight Client

This is the easy part. We’ll use the RIA Services drag and drop features. Open MainPage.XAML in the Silverlight application and in the Data Sources window, drag and drop the Customer entity to the XAML form. (Tip: if the Data Sources window is blank or not showing up, you can manually force it to come up via the “Data” menu option on the main menu in Visual Studio.)

Once you drag and drop the entity to the form, a grid will automatically show up.

image

Now press F5 and see the application running.

image

That's it! We just created an OpenAccess based RIA Services application!

Of course there is a lot more to RIA Services than just binding a grid, however, this demonstration should show you that once you create your DomainService class, all of the RIA Services “stuff” just works for you. In future posts we will look at more RIA Services features as well as creating a query method.

Enjoy!


About the Author

Steve Forte

 sits on the board of several start-ups including Triton Works. Stephen is also the Microsoft Regional Director for the NY Metro region and speaks regularly at industry conferences around the world. He has written several books on application and database development including Programming SQL Server 2008 (MS Press).

Comments

Comments are disabled in preview mode.