Friday, August 2, 2013

OData continued

To create an OData service, here are some of the steps to take:
1) Have the data ready say in a database and test the connection string.
2) Next make a web project with an entity data model, WCF works well with EF
3) Update the model from the database
4) For the API you want to add, create an association by right clicking on the entity and add association
5) Right click on the project and add new Item and WCF data service
6) In the service class : specify the type with the base DataService and in the InitializeService method add the config.SetEntitySetAccessRule
7) Add the JSONPsupportBehaviour attribute to the service class if desired.
8) Test the service by navigating to http://<yoursite>/service.svc
OData is not limited to SQL Databases. And it is definitely not about putting your database on the web. You choose which entities are accessed over the web and you can expand the reach with OASIS standard. OASIS is a global consortium that drives the development, convergence and adoption of web standards.
OData is not limited to WCF services.
In fact, the Asp.Net WebAPI framework makes it easy to author RESTful APIs out of the box. While the WCF DataService wraps the context and locks down the interactions such that we only set the permissions in the constructor to the DbSets from the context, the Asp.Net WebAPI, on the other hand, lets us define the interactions and we define the logic in the methods. Therefore we are need not use EntityFramework. This is the key difference between choosing a service over an API to expose the OData. Thus the CRUD functionality alone calls for a service and anything more calls for an API. Experts suggest that if you are working from the data upwards to expose it, you could merely use the service. If you are working from the clients via methods and want to control access to the model, you choose the API.  In the WebAPI framework, the App_Data, models and views could be deleted.
Using Asp.Net WebApi, the endpoints are created easily Many endpoints are created and they can be hosted side by side with non-OData endpoints. The Framework does limit control over any of the layers such as the data model, the back-end business logic and data layer
To create an endpoint with this framework, the following steps should be taken :
Create an MVC controller just the same as a standard one where you use the scaffolding options to point to the model and the Data context class.
The above step generates the WebAPI controller with the corresponding GET api/entity methods .
JSON is the default response type unless XML is specifically requested.
PUT methods are also provided to update the entities.
Next, we convert the controller into an OData Controller by deriving from EntitySetController and specify the entity as the associated type.
Next we add the methods we want to expose and leave out the ones we want to restrict. Notice that this is different from the SetEntitySetAccessRule we use with the DataService.
Next we define a route to find the OData with config.Routes.MapODataRoute("ODataRoute", "odata", model);
And then we rename the controller class. The data can be consumed from clients in say Javascript, PHP or python. It can be tested with Fiddler and curl.


No comments:

Post a Comment