Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

We have a WCF application service layer which acts as DataService for our domain models defined in DB. We are writing new REST web services that will work on top of existing application service. Planning to use MVC Web API.

We want to expose our RESTFul APIs as OData endpoint. But looks like OData is tightly coupled with EnityFramework Data Model and Context.

The problem is we can't use EF dbcontext as we need to create objects by requesting them from backend application service and then map to the data model.

Is there a way to implement OData without DB but application service as data source.

Thanks, M

share|improve this question
    
MVC and WebAPI are two separate things –  LostInComputer Jan 17 '14 at 8:11

2 Answers 2

Looking though the MSDN article introducing OData, it seems that while the scaffolding in Visual Studio is aimed at using OData with EF, you can still create controllers directly by deriving from EntitySetController:

public class PeopleController : EntitySetController<Person, int>
{
    public override IQueryable<Person> Get()
    {
        // return your own non-EF data source
    }
}

As long as you can get an IQueryable, you should be able to knock up an OData controller.

share|improve this answer

You can! The only problematic action you have to take care of is the get.

There are two ways:

  1. Use ODataQueryOptions. It contains the parameters like skip, orderby, etc.

Sample

public class TestController : ODataController
{
    public IEnumerable<TestModel> Get(ODataQueryOptions<TestModel> options)
    {
        var entities = new List<TestModel>()
        {
            new TestModel { Id = 1 },
            new TestModel { Id = 2 },
            new TestModel { Id = 3 },
            new TestModel { Id = 4 },
            new TestModel { Id = 5 },
            new TestModel { Id = 6 }
        };

        //In this example, we use ApplyTo but you can build an adapter to your application service from the options parameter
        return (IEnumerable<TestModel>)options.ApplyTo(entities.AsQueryable());
    }
}
  1. Implement your own IQueryable Provider that will query the application service. I didn't try this but I don't see any reason for it to not work.
share|improve this answer
    
This answer is for Web API 2 –  LostInComputer Jan 17 '14 at 8:09
    
This is helpful. Can it handle relationship queries between multiple objects ex. localhost/odata/Product(1)/Supplier/Vendor here we want to get vendor of Supplier for product 1. –  MarvinDev Jan 17 '14 at 10:05
    
That is another/unrelated question. /odata/Product(1)/Supplier is supported automatically by the ODataConventionModelBuilder. For /odata/Product(1)/Supplier/Vendor, I unfortunately don't know how to do it. A workaround is to use $expand (odata/Product(0)/Supplier?$expand=Vendor) which will include Vendor in the result –  LostInComputer Jan 17 '14 at 15:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.