Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it even a good idea to use OData?

Understanding ahead of time the caveat that "just because a capability is offered doesn't make it a good idea"...

From the looks of it, OData compliant signatures require you return IQueryable.

FOR EXAMPLE:

[Queryable]
public IQueryable<MyModel> Get()
{
    return _repo.GetAll().AsQueryable();
}

However, many articles in the recent and not-so-recent past describe IQueryable as:

  • Leaky
  • 'Causing Heavy Lifting' example: allows users to grab entire table(s)
  • Has security & scaling issues as it allows for heavy modification of the query itself
  • Can also cause issues from deferred execution (because of its' queryable nature)

MY QUESTION IS:
Do you feel the capabilities of IQueryable and OData outweight the issues above?

In answering, I would expect people to talk about:

  • Why or why not?
  • When should I use it?
  • Do you only use in some WebAPI calls...or for all WebAPI calls?
  • What about individual models which are not IEnumarable objects?

...things like that.

BACKGROUND: I ask not only because of the items listed above. But also because OData is being sold to us as an "industry standard" rather than a tool in your toolbox. So, implementing this would fundamentally change the returns for our WebAPI calls (where I currently work). We would have to go from our own IResult return signature (which is very useful) to IQueryable which seems to have issues (but may also end up useful).

IRESULT EXAMPLE:
At minimum, our return signatures would change drastically. And, I'm told WebAPI call implementing OData wont work by changing "C Instance" to "IQueryable Instance" (which makes sense).

public interface IResult<C>
{
    [JsonProperty(PropertyName = "hasErrors")]
    bool HasErrors { get; }

    [JsonProperty(PropertyName = "errors")]
    IList<String> Errors { get; }

    [JsonProperty(PropertyName = "instance")]
    C Instance { get; set; }
}
like image 862
Prisoner ZERO Avatar asked Mar 13 '13 16:03

Prisoner ZERO


People also ask

Should I use OData?

OData helps you focus on your business logic while building RESTful APIs without having to worry about the various approaches to define request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats, query options, etc.”

Why is OData not popular?

There is virtually no contract. A service consumer has no idea how to use the service (for example, what are valid Command arguments, encoding expectations, and so on). The interface errs on the side of being too liberal in what it will accept.

Why do we need OData?

The purpose of OData is to provide a protocol that is based on Representational State Transfer (REST) for create, read, update, and delete (CRUD) operations. OData applies web technologies such as HTTP and JavaScript Object Notation (JSON) to provide access to information from various programs.

Is OData same as REST API?

REST is an architectural style for exchanging information via the HTTP protocol. The REST standard defines principles that must be adhered to by any REST API. OData builds on top of the REST framework to define best practices for building REST APIs—including the HTTP message format, how to query the API, and more.


1 Answers

Supporting OData querying with Web API doesn't require you to have an IQueryable<T>. Having an IQueryable<T> lets you get there faster and with lesser code. IQueryable<T> has the required abstractions to translate an incoming OData query to a LINQ query. The framework already defines it and there is rich support across various back ends for it like Entityframework, NHibernate, Linq2Objects, RavenDB, Linq2OData etc. So, we decided to have rich support for IQueryable<T> with web API. Agreed, IQueryable<T> is a huge interface and exposes a lot more than what is necessary for OData querying. But it is something that comes for free :).

That said, we have good support through ODataQueryOptions<T> for non-IQueryable case. Check out my blog post about this here

Also, you are confusing OData query semantics with OData. Rich query support is only one part of OData. OData builds on top of HTTP and has various useful features like

  • A well thought out application of both REST and HTTP best practices.
  • Unambiguos representations of your resources in json and xml.
  • $metadata.
  • A standard way of describing relationships.
  • Rich querying that supports projections, filtering, client driven paging and server driven paging(next page link).
  • Big ecosystem.
like image 93
RaghuRam Nadiminti Avatar answered Oct 02 '22 20:10

RaghuRam Nadiminti