CRUD Operations On OData Endpoint Using Web API

Introduction

OData is an open protocol. Currently OData is widely used for exposing data. OData is operating data over HTTP. It follows REST architecture. In my previous article, we learn how to configure OData endpoint with Web API, Filter expression and paging in OData with web API. Other important functionally in any API is to perform CRUD (Create, Read, Update and Delete) operation. In this article, we will learn how to perform CRUD operation with OData end point.

Example:

I have taken same example which were used in my previous article. In this example, I have created a static data source. Here I have created class that holds collection of TestData class and I will perform CRUD operation on this static collection.

Static data source definition

  1. namespace WebAPITest  
  2. {  
  3.   using System.Collections.Generic;  
  4.   public class DALTestData  
  5.     {  
  6.        public static List<TestData>Testdata = newList<TestData>();  
  7.        static DALTestData()  
  8.         {  
  9.           Testdata.Add(newTestData { Id = 1, Name = "Jignesh", Role = "Project Manager" });  
  10.           Testdata.Add(newTestData { Id = 2, Name = "Tejas", Role = "Architect" });  
  11.           Testdata.Add(newTestData { Id = 3, Name = "Rakesh", Role = "Lead" });  
  12.           Testdata.Add(newTestData { Id = 4, Name = "Hiren", Role = "Developer" });  
  13.           Testdata.Add(newTestData { Id = 5, Name = "Pooja", Role = "Developer" });  
  14.           Testdata.Add(newTestData { Id = 6, Name = "Keyur", Role = "Developer" });  
  15.           Testdata.Add(newTestData { Id = 7, Name = "Ashish", Role = "Developer" });  
  16.           Testdata.Add(newTestData { Id = 8, Name = "Parth", Role = "Developer" });  
  17.           Testdata.Add(newTestData { Id = 9, Name = "Manish", Role = "QA" });  
  18.           Testdata.Add(newTestData { Id = 10, Name = "Manisha", Role = "QA" });  
  19.           Testdata.Add(newTestData { Id = 11, Name = "Urmi", Role = "QA" });  
  20.           Testdata.Add(newTestData { Id = 12, Name = "Bharat", Role = "QA" });  
  21.           Testdata.Add(newTestData { Id = 13, Name = "Varun", Role = "QA" });  
  22.           Testdata.Add(newTestData { Id = 14, Name = "Komal", Role = "QA" });  
  23.           Testdata.Add(newTestData { Id = 15, Name = "Dhaval", Role = "QA" });  
  24.           Testdata.Add(newTestData { Id = 16, Name = "Chirag", Role = "Project Manager" });  
  25.           Testdata.Add(newTestData { Id = 17, Name = "Devid", Role = "QA" });  
  26.           Testdata.Add(newTestData { Id = 18, Name = "Vivek", Role = "Developer" });  
  27.           Testdata.Add(newTestData { Id = 19, Name = "Purvi", Role = "Architect" });  
  28.           Testdata.Add(newTestData { Id = 20, Name = "Denish", Role = "Developer" });  
  29.         }  
  30.     }  
  31. }  
To test the application, I am using Telerik's Fiddler. It’s free. Using Fiddler, we can analyze the input / output of URL and also able to POST data without any user interface.

Create Operation (CRUD)

POST verb is used for adding an item to the data source. It  generally accepts data from client and post data over HTTP in request body.

To catch the POST request I am using following code. Here I am adding the posted data to the static collection.
  1. public HttpResponseMessage Post([FromBody]TestData entity)  
  2. {  
  3.     int id = DALTestData.Testdata.Max(p =>p.Id) + 1;  
  4.     entity.Id = id;  
  5.     DALTestData.Testdata.Add(entity);  
  6.     return Request.CreateResponse(HttpStatusCode.OK, entity);   
  7. }  
Output

Output

As we see here, the POST method returns HttpResponseMessage object along with newly create entity object in body.

Read Operation (CRUD)

GET verb is also being used to retrieve a specific item; we just required additional information such as key in the URI. Following code can be used to retrieving a specific item.
  1. public HttpResponseMessage Get([FromODataUri]int key)  
  2. {  
  3.   TestData data = DALTestData.Testdata.Where(k =>k.Id == key).FirstOrDefault();  
  4.   if (data == null)  
  5.     {  
  6.        returnRequest.CreateResponse(HttpStatusCode.NotFound);  
  7.     }  
  8.   
  9.    returnRequest.CreateResponse(HttpStatusCode.OK, data);  
  10. }  
OData supports many query options (filter expression), which are use to query data from client. The FromODataUri attribute used to tell web API action method that pickup the value from requested URL.

URI:http://localhost:24367/TestData(1)

Output in Fiddler

Output in Fiddler

Update Operation (CRUD)

PATCH or PUT verb is used for updating an item to the data source. The main difference between PATCH and PUT is that PATCH is use to perform partial update and PUT replaces entire entity with the new received object.

We can write either one of these two or both methods depending on the requirement.

PUT Example:

The client PUTS following method example that modifies the entity selected by using key send.
  1. public HttpResponseMessage Put([FromODataUri]int key, [FromBody]TestData entity)  
  2. {  
  3.   TestData data = DALTestData.Testdata.Where(p =>p.Id == key).FirstOrDefault();  
  4.   if (data != null)  
  5.      {  
  6.        data.Name = entity.Name;  
  7.        data.Role = entity.Role;  
  8.     }  
  9.   else  
  10.     {    
  11.         Request.CreateResponse(HttpStatusCode.NotFound);  
  12.     }  
  13.   return Request.CreateResponse(HttpStatusCode.OK, data);  
  14. }  
Output

Output

PATCH Example

The Delta class is able to track changes for the entity. Here I am using CopyChangedValues method in PATCH. This method copies the changed properties value from underlying entity.
  1. public HttpResponseMessage Patch([FromODataUri]int key, [FromBody]Delta<TestData> entity)  
  2. {  
  3.   TestData data = DALTestData.Testdata.Where(p =>p.Id == key).FirstOrDefault();  
  4.   if (data != null)  
  5.     {  
  6.       entity.CopyChangedValues(data);  
  7.     }  
  8.  else  
  9.     {    
  10.       Request.CreateResponse(HttpStatusCode.NotFound);  
  11.     }  
  12.   returnRequest.CreateResponse(HttpStatusCode.OK, data);  
  13. }  
Output:

output

Delete Operation (CRUD)

The DELETE method is very straightforward. This method is just accepts a key and based on the key we need write logic to remove entity.
  1. public HttpResponseMessage Delete([FromODataUri]int key)  
  2. {  
  3.   TestData data = DALTestData.Testdata.Where(p =>p.Id == key).FirstOrDefault();  
  4.    if (data != null)  
  5.      {  
  6.         DALTestData.Testdata.Remove(data);  
  7.      }  
  8.    else  
  9.      {  
  10.         Request.CreateResponse(HttpStatusCode.NotFound);  
  11.      }  
  12.   return Request.CreateResponse(HttpStatusCode.OK, "Removed");  
  13. }  
output

Summary

OData provides way to query a data set and supports CRUD (create, read, update, and delete) operations. OData provides many useful filtering options.

Next Recommended Readings