1
votes

Context:

I have an existing Web API of the following form: http://site/api/{area}?slicer1=alpha&slicer1=beta&starttime=sometime&endtime=sometime

It's implemented in ASP MVC. The API's function processes the parameters and feeds them into a SQL query. On success, it returns a IHttpActionResult with JSON data from SQL.

Note that the API lacks an entity model or entity relationship diagram. It's essentially just a wrapper around a SQL query.

Question:

Recently, I started learning about OData. It seems like OData is designed for the URL itself to control how data is filtered, as opposed to authoring a custom SQL query to filter.

Hence, I'd like an answer on whether my Web API could be converted to an OData API and, if so, what OData capabilities I'd need to do that (for instance, might OData v4 functions be useful)?

Clarifications:

  1. I don't have any code written, nor am I asking for code as an answer.
  2. I am looking to know what OData capabilities might enable my scenario (v4 functions, actions, etc...), or if OData and Web APIs are so different that my ask doesn't make sense.
  3. Anticipating the "Why are you asking" this questions - I'm just interested in technically feasibility as a learning exercise.
1

1 Answers

2
votes

You could switch to a OData API but if you have no entities, i.e. no IQueryable to query, you'd still have to do the SQL command generation by yourself. It's not too hard though - we did it in the project I'm working in.

You also have to ask yourself to what extent you want to switch to OData. For example, you could decide to just use its (type-safe) query-string parsing capabilities and from the parsed filter tree generate your own SQL (as mentioned above).

On the other hand, having a fully fledged OData API would also dictate the response format to comply to the standard. This would mean that you can start connecting to your API using OData aware tools (e.g. Excel, KendoUI Grid, etc.). I don't know if that would give you any benefit for your use-case.

Your question

what OData capabilities I'd need to do that

is not exactly clear to me. There are no OData capabilities that would help you to migrate from a Web API to an OData API. OData is just a set of standardized query (CRUD) and response formats. You can also be OData compliant by implementing all on your own using out-of-the-box Web API facilities but probably you'd want to use the Web API OData package.

The most important question for you is to ask yourself what advantage you'd have using OData.