Search code examples
c#wcfdynamic-dataodata

Is there an easier way to create a WCF/OData Data Service Query Provider?


I have a simple little data model resembling the following:

InventoryContext {

IEnumerable<Computer> GetComputers()

IEnumerable<Printer> GetPrinters()

}

Computer {

public string ComputerName { get; set; }

public string Location { get; set; } }

Printer {

public string PrinterName { get; set; }

public string Location { get; set; }

}

The results come from a non-SQL source, so this data does not come from Entity Framework connected up to a database.

Now I want to expose the data through a WCF OData service. The only way I've found to do that thus far is creating my own Data Service Query Provider, per this blog tutorial:

http://blogs.msdn.com/alexj/archive/2010/01/04/creating-a-data-service-provider-part-1-intro.aspx

... which is great, but seems like a pretty involved undertaking. The code for the provider would be 4 times longer than my whole data model to generate all of the resource sets and property definitions.

Is there something like a generic provider in between Entity Framework and writing your own data source from zero? Maybe some way to build an object data source or something, so that the magical WCF unicorns can pick up my data and ride off into the sunset without having to explicitly code the provider?


Solution

  • You can use the built in Reflection Provider.

    Add the following to your InventoryContext:

    IQueryable<Computer> Computers { get { return GetComputers().AsQueryable(); } }
    IQueryable<Printer> Printers { get { return GetPrinters().AsQueryable(); } }
    

    And modify the entities as follows (you'll need to add a reference to System.Data.Services.Client to your project):

    using System.Data.Services.Common;
    
    [DataServiceKey("ComputerName")]
    public class Computer 
    {
        public string ComputerName { get; set; }
        public string Location { get; set; } }
    }
    
    [DataServiceKey("PrinterName")]
    public class Printer
    {
        public string PrinterName { get; set; }
        public string Location { get; set; } }
    }
    

    Once you've done that, just point your data service at your InventoryContext, like this:

    public InventoryDataService : DataService<InventoryContext>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.UseVerboseErrors = true;         
        }
    }
    

    That should be all you need to do. The InventoryContext will need to have a parameterless constructor.