A simple use case with OData Web Api

Scenario :

Assume a case where we have an Employee object and we have an nullable  integer named Age (int? Age).  There are two cases we want to achieve,the first case is to not update the Age property and we could achieve this by passing null value to Age and ignoring the Age Property while saving and second case is to reset the Age i.e Accept whatever is passed by Age Property and save it in db.

Solution :

We want to only update properties of Employee object that is being passed to the controller and we call it as “partial update”(PATCH).  In this case we update only what is being passed and leave the system unchanged. Microsoft provides OData for partial update.

  1. Get Microsoft ASP.NET Web API OData package from Nuget.
  2. In your controller  HTTP verb Method,you could make use of Delta<T>  to map your Employee object and this will take care of partial update with Patch Method provided by ODATA .

public ApiResponse<Statuscode> Patch(Delta<Employee>; employee)

{

var employeebyId == dbcontext.FirstorDefault(x=>x.EmpId ==employee.EmpId);

if(employee !=null)

{

employee.Patch(employeebyId ); // Partial update by ODATA

}

}

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s