OData without IQueryable - asp.net-web-api

I’m thinking of using OData for my web service (based on Web API). Unfortunately, my datasource is NOT IQueryable. Instead of implementing my own IQueryable I pretty much followed this blog post.
What I don’t understand is how to get to my entity data model (EDM)? Do I have to model an EDM for example in the Designer? Or is this for EF only and I can use „plain“ classes instead and set relation-attributes? I don’t want to expose my internal data structures therefore my EDM is more like DTOs...
In an example I’ve seen that I‘m supposed to derive from EntitySetController in order to get the OData-compliant HTTP response. I believe I can’t use EntitySetController as I don’t support IQueryable. What am I supposed to do in order to get a proper response anyway?

You can build an EDM model yourself. You can use the ODataConventionModelBuilder class to build your EDM model. This tutorial has some sample code.
Also, checkout the samples on OData from here, especially the ODataServiceSample and ODataCompositeKeySample. They should get you started.
Also, if you do not have an IQueryable, you could derive from ODataController instead of EntitySetController.

Related

using webapi odata without using ODataConventionModelBuilder

I have written a apicontroller class that supports ODATA by using the EnableQuery attribute on the methods and returning the data asQueryable. It alls works fine for me.
What use is the ODataConventionModelBuilder? Do I still need to use it?
The using of ODataConventionModelBuilder enables
The mapping from your POCO classes to the OData model including handling primitive types, complex types, entities types and relationships between them, actions, function, and so on.
It also helps validate if you have a correct OData model.
It helps set up the metadata document and service document.
If you really want to expose an OData service that conforms the OData protocol, you would need to do a lot yourselves without the ODataConventionModelBuilder. But if you don't care about things like conforming model conventions or exposing the service model to the service consumers, you may not use it.

Queryable Web API 2 OData serialization

I have a lighweight setup where I am using Web API 2 with OData but do not create an "/odata" endpoint with its accompanying "/odata/$metadata" but instead just use standard ApiController's with attribute routing and Get methods marked with an override of [Queryable]: [InlineCountQueryable] detailed here Web API, OData, $inlinecount and testing.
It all works very well except that I can only expose dedicated model classes without any collections or object properties because the JSON serializer serializes the whole object graph. My ideal behavior would be to just have all collections ignored completely and object properties only included on $expand.
I believe this can be made to work because an OData Web Api 2 scaffold VS 2013 creates over an entity framework context has similar behavior, but I would like to keep this as lean as possible and avoid going that route as I found it to be a bit brittle and this service does not need to expose a $metadata description.
Update
I did some digging in the aspnet samples and found the "CustomODataFormatter" sample which seems to expose the machinery I would need to modify.. however the ODataSerializerProvider/ODataSerializer setup outlined there does not work with with attribute routing at all (any method defined with [Route("Some/Url")]).
Given that this seems to be what I need, does anyone know if attribute routing and the odata serializer provider mechanism should work together?
[JsonIgnore] or [DataMember] can be used to ignore properties for json.net serializer. webapi.odata 5.2 supports ODataRouteAttribute, an example is here.

Advice on implementing odata for non-EF entities in asp webapi

We are planning to use oData as a entity standard for rest api layer. The data provided by web api are taken from various subsystems and layers of cache(memcache) involved. My fear is how the basic entity model without EF can support the oData implementation of pagination and filtering. I want the ability to restrict the retrieval record limit at the underlying susbsytem level not at the api controller level. Any pointers would be useful. Thanks.
I have written a blog post on doing OData querying over non-IQueryable backends. I have picked HQL as the target query language. http://blogs.msdn.com/b/webdev/archive/2013/02/25/translating-odata-queries-to-hql.aspx
That should help you to understand how easy it is to support OData queries using web API.

Entity Framework POCO Serialization

I will start to code a new Web application soon. The application will be built using ASP.Net MVC 3 and Entity Framework 4.1 (Database First approach). Instead of using the default EntityObject classes, I will create POCO classes using the ADO.NET POCO Entity Generator.
When I create POCOs using this tool, it automatically adds the Virtual keyword to all properties for change tracking and navigation properties for lazy loading.
I have however read and seen from demonstrations, that Julie Lerman (EF Guru!) seems to turn off lazy loading and also modifies her POCO template so that the Virtual keyword is removed from her POCO classes. Julie states the reason why she does this is because she is writing applications for WCF services and using the Virtual keyword with this causes a Serialization issue. She says, as an object is getting serialized, the serializer is touching the navigation properties which then triggers lazy loading, and before you know it you are pulling the whole database across the wire.
I think Julie was perhaps exagarating when she said this could pull the whole database across the wire, however, even so, this thought scares me!
My question is (finally), should I also remove the Virtual keyword from my POCO classes for my MVC application and use DectectChanges for my change tracking and Eager Loading to request navigation properties.
Your help with this would be greatly appreciated.
Thanks as ever.
Serialization can indeed trigger lazy loading because the getter of the navigation property doesn't have a way to detect if the caller is the serializer or user code.
This is not the only issue: whether you have virtual navigation properties or all properties as virtual EF will create a proxy type at runtime for your entities, therefore entity instances the serializer will have to deal with at runtime will typically be of a type different from the one you defined.
Julie's recommendations are the simplest and most reasonable way to deal with the issues, but if you still want to work with the capabilities of proxies most of the time and only sometimes serialize them with WCF, there are other workarounds available:
You can use a DataContractResolver to map the proxy types to be serialized as the original types
You can also turn off lazy loading only when you are about to serialize a graph
More details are contained in this blog post: http://blogs.msdn.com/b/adonet/archive/2010/01/05/poco-proxies-part-2-serializing-poco-proxies.aspx
Besides this, my recommendation would be that you use the DbContext template and not the POCO template. DbContext is the new API we released as part of EF 4.1 with the goal of providing greater productivity. It has several advantages like the fact that it will automatically perform DetectChanges so that you won't need in general to care about calling the method yourself. Also the POCO entities we generate for DbContext are simpler than the ones that we generate with the POCO templates. You should be able to find lots of MVC exampels using DbContext.
Well it depends on your need, if you are going to serialize your POCO classes than yes you should remove them (For example: when using WCF services or basically anything that will serialize your entire object). But if you are just building a web app that needs to access your classes than I would leave them in your classes as you control the objects that you will access in your classes through your code.

How do I Integrate Entity Framework with External REST Data Source?

I am creating my first ASP.NET MVC 3 application, and my data comes from a data source I can access only via its REST API.
I will only be using READ-ONLY access at this point to the REST data source (no updating, etc.)
I would like to use the Entity Framework V4 to provide a Business Entity interface to MVC 3 without exposing it to the REST API.
I need to get something working quickly - so I don't have time to fully understand the Server Layer / UnitOfWork and Repository patterns just yet, although I plan to go there next.
I am willing to use a Repository class at this time, but not ready for DI / IoC container yet.
Any suggestions on where the RESP API calls go?
EDIT
Learned by asking this question that it is not necessarily useful to integrate an ORM with a REST API - See my accepted answer below.
An Object/Relational Mapper, or ORM, like Entity Framework has specifically been developed to abstract away a relational database. It might not be the right fit for REST calls.
You could instead build a repository class that encapsulates the REST call and exposes methods like IEnumerable<T> GetAll() or T GetyById(...).

Resources