The One With

OData Provider for XPO - Introduction

The Open Data Protocol (OData) is quickly becoming the format of choice when it comes to exposing data. The growing number of client side libraries and all the new features from WCF Data Services is what makes the developers switch from proprietary (Custom REST APIs, SOAP, Binary Formats etc…) to OData.

The simplest way to expose your data as OData is by using the WCF Data Services, which currently comes with a couple of out of the box providers (EF, Reflection etc…). Each provider requires it’s own data model and if you have made XPO your ORM of choice, switching to another model is out of the question. Adding an additional data layer is not something you would want to do either, maintaining more than one is rarely a good practice.

The cool thing about the WCF Data Services is that you can plug in custom providers. A provider for XPO can be downloaded from CodePlex. We’ll develop it together in this series of blog posts, but first, I want to show you how easy it is to expose your XPO model using the XPO Data Service Provider.

Step 1: Have your XPO Model Ready

For demonstration purposes my persistent objects are mapped to the Nwind access database. I used the Add New Persistent Classes Wizard to generate mine.

public class Customers: XPLiteObject {
    string fCustomerID;
    [Key]
    [Size(5)]
    public string CustomerID {
        get { return fCustomerID; }
        set { SetPropertyValue<string>("CustomerID", ref fCustomerID, value); }
    }
    ...
}

public class Products : XPLiteObject {
    int fProductID;
    [Key(true)]
    public int ProductID {
        get { return fProductID; }
        set { SetPropertyValue<int>("ProductID", ref fProductID, value); }
    }
    ...
}

Step 2 : Add a new WCF Data Service to your project

public class Nwind : DataService< /* TODO: put your data source class name here */ >
{
    // This method is called only once to initialize service-wide policies.
   
public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
        // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
       
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

Step 3: Add a Reference to DevExpress.Xpo.Services.v10.1.dll

Step 4: Configure your WCF Data Service

[XpoConnectionString("Nwind")]
public class Nwind : XpoDataService
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}

File: Web.config

<connectionStrings>
  <
add name="Nwind" connectionString='Provider=Microsoft.Jet.OLEDB.4.0;Data Source="nwind.mdb"'/>
</
connectionStrings>

And that’s it! We can now explore our data using a number of different clients that support OData or simply view the feeds in the browser.

OData Feed for XPO

What’s Next?

Cheers

Azret

Published May 21 2010, 02:28 PM by
Bookmark and Share

Comments

Christian Desrosiers

Great news!

Could this be used as a replacement for AgXpo in a Silverlight application?

May 25, 2010 10:57 AM

Azret Botash (DevExpress)

Christian, AgXPO uses a different approach and protocol for addressing data. I will touch on this later...

May 27, 2010 12:51 PM
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners