Friday, September 03, 2010    
Blog  
OpenLight Blog
May 15

Written by: Michael Washington
5/15/2010 9:36 AM 

Will OData eliminate the need for standard web services? Here is a simple example of how OData works. This example does not use a database connection so that you can easily see just the OData parts.

 

 

In Visual Studio 2010, create a New Project.

 

 

Make an Empty ASP.NET Web Application.

 

 

Add a New Item...

 

 

Add a WCF Data Service

Paste in the following code:

 

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace ODataSample
{
    public class Service : DataService<SampleDataSource>
    {
        // This method is called only once to initialize service-wide policies.  
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.MaxResultsPerCollection = 100;
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }

    [EntityPropertyMappingAttribute("CustomerName", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, true)]
    [DataServiceKey("CustomerID")]
    public class CustomerRecord
    {
        public int CustomerID { get; set; }
        public string CustomerName { get; set; }
        public string CustomerEmail { get; set; }
        public string CustomerNotes { get; set; }
        public DateTime CustomerLastContact { get; set; }
    }

    public class SampleDataSource
    {
        private readonly List<CustomerRecord> _sampleCustomerRecordList;
        public SampleDataSource()
        {
            _sampleCustomerRecordList = new List<CustomerRecord>();

            for (int i = 0; i < 100; i++)
            {
                CustomerRecord CR = new CustomerRecord();

                CR.CustomerID = i;
                CR.CustomerName = string.Format("FirstName{0} LastName{1}", i.ToString(), i.ToString());
                CR.CustomerEmail = string.Format("Email{0}@{1}.com", i.ToString(), i.ToString());
                CR.CustomerNotes = string.Format("Notes {0}. Notes {1}", i.ToString(), i.ToString());
                CR.CustomerLastContact = DateTime.Now.AddDays(-10000).AddHours(i);

                _sampleCustomerRecordList.Add(CR);
            }
        }

        public IQueryable<CustomerRecord> SampleCustomerData
        {
            get
            {
                return _sampleCustomerRecordList.AsQueryable();
            }
        }
    }
}

Save and build the site.

 

 

Right-click on the Service in the Solution Explorer

 

 

You will see the service.

Download and install LinqPad (http://LinqPad.com)

 

 

In LinqPad, add a new connection 

 

 

Make a WCF Data Services connection

 

 

Paste in the URL to the service

 

 

You will see the Entities in the service

 

You can execute queries against the OData service. The query above gets the first 20 records that match the query.

 

The query above gets the second page.

OData Security

To secure your OData points see:

Simple Example To Secure WCF Data Service OData Methods

Download:

ODataSample/ODataSample.zip

Tags:

5 comment(s) so far...

Re: OData Simplified

Hi.

Missing generic type parameter for DataService. Should be: public class Service : DataService { ... }
This applies only to the text above. Sample code is ok.

Przemek

By Przemyslaw Soszynski on   5/20/2010 4:16 AM

Re: OData Simplified

@Przemek Thank you for letting me know about the bug. The code formatter dropped it out. the code is fixed now.

By Michael Washington on   5/20/2010 4:17 AM

Re: OData Simplified

This is a very good post, thanks for sharing this. It boils it down to the very simple concepts, and shows that the backend need NOT be implemented on top of Entity Framework only, or any specific technology for that matter.

By Josh Gough on   6/12/2010 6:11 PM

Re: OData Simplified

In the LinqPad's DataService conection window you can enter logon details. If you do, what is going to happen? Will there be a header added to the HTTP request (possibly Authorization), a cooki will be added, or what? And what can you do on the server side to implement authentication? Create a managed aspx module to verify the credentials?

By Przemyslaw Soszynski on   6/17/2010 9:19 AM

Re: OData Simplified

@ Przemyslaw Soszynski - I never tried to see what the logon details does. There are forums for LinqPad at: forums.oreilly.com/category/22/C-3-0-in-a-Nutshell/ that should be able to answer that.

By Michael Washington on   6/17/2010 9:23 AM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use