5
votes

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?

3
There's an easier way to ask if something is more easy. :)Phil Gan

3 Answers

2
votes

You can use so called "reflection provider". This assumes that you have a property (or many properties) which returns IQueryable (T being your entity type). Take a look at this video for a simple "How to" to get you started. http://msdn.microsoft.com/en-us/data/cc745968.aspx

1
votes

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.

0
votes

You can use the WCF Data Services toolkit.

It was written originally by a group of guys from Microsoft and allows a very flexible way of creating OData services that are not based on Entity Framework.

Jonathon Carter did an excellent demo at mix last year wrapping an OData service around Mongo DB.

http://channel9.msdn.com/events/MIX/MIX11/FRM16

The code is also available on codeplex http://wcfdstoolkit.codeplex.com/

Hope this helps.