CRUD Operations With WCF Odata Service


In my previous article, I explained:

  1. What is Odata?
  2. Use of Odata service
  3. Limitations of Odata
  4. Creating basic WCF Odata service using Visual studio 2010.

In this article, I want go into depth about Odata regarding CRUD operation, Custom entity etc.

Before going into depth, it would be better to explain about REST service.

Representational state transfer (REST) is a style of software architecture for World Wide Web. The cool thing about REST is that it is not tied to any particular technology or platform. The term is often used in a looser sense to describe any simple interface which transmits domain-specific data over HTTP without an additional messaging layer such as SOAP or session tracking via HTTP cookies. A RESTFul web service is a simple web service implemented using HTTP and the principles of REST. Such a web service can be thought of as a collection of resources.The MIME type of the data is supported by the web service. This is often JSON, XML but can be anything. The set of operations are supported by the web service using HTTP methods (e.g. POST, GET, PUT or DELETE).

WCF Rest Services

WCF Rest Services are normal WCF Services that have added functionality so that they can be consumed in a RESTful manner.

HTTP Verbs supported by WCF Data services are:

  1. GET- To request a specific representation of a resource.
  2. PUT- To create/ update a resource.
  3. Delete- To delete a resource.
  4. POST- Submits data to be processed by the identified resource.
  5. HEAD- Similar to GET, but retrieves only header part and not the body.

Let me to start with OData crud operations.

I have created one OdataDemo Silverlight application. So, I am going to start with Create/Add new resource to the entity and DB.

There are two ways to do CRUD operations:

  1. Using entity object.
  2. DataServiceContext object.

Just like given below:

DataServiceContext context = new DataServiceContext(new Uri("http://localhost:60259/ODataDemoService.svc/"));
 
ODataDemoEntities ObjEntity = new ODataDemoEntities(new Uri("http://localhost:60259/ODataDemoService.svc/"));


I have written code for both using an entity as well as a datacontext object.

  1. Create/Add operation.

    CRUDWCF1.gif
     
  2. Retrieve resource/resources from DB.

    CRUDWCF2.gif
     
  3. Update operation.

    CRUDWCF3.gif

    Because Silverlight 4 is used, I have used an asynchronous call back. The next version of Silverlight (Silverlight 5) does not require a callback method.
     
  4. Delete operation.

    CRUDWCF4.gif

In this way we can CRUD operations in WCF Data Service and Silverlight Client.