jeudi 21 septembre 2017

Update method in web API: How to know which fields to update?

I have a typical web API with a couple of PUT/UPDATE endpoints. These endpoints simply call the underlying service, and do the update.

The service layer, has the typical signature such as Object Update(Object object). What I then do is I basically run the following pseudo code:

var dbobject = _db.Object.Find(object.Id);    
dbobject.Field1 = object.Field1;
dbobject.Field2 = object.Field2;
// continue for all fields

_db.SaveChanges();

return GetObjectById(object.Id);

However, this provides a challenge for me.

Lets say we have a consumer of our API. This consumer calls my PUT endpoint (/api/Object/{id}), and the payload is the updated Object.

However, lets say that the object we put don't know about example Field4, then this value would be NULL after the update has been run.

My question is:

  • What do you do about all those fields the payload does NOT contain? How do you handle not setting values to NULL you don't expect to be NULL afterwards?

Aucun commentaire:

Enregistrer un commentaire