I am writing a WCF Rest API that provides CRUD functionality to an entity say Student. Here is how it is defined:
class Student
{
Guid Id;
string Name;
string LastName;
string Age;
DateTime DOB;
}
My service contract is something like this:
class StudentService
{
[WebInvoke(Method = "POST")]
public void Add(Student student)
{...}
[WebInvoke(Method = "PUT")]
public void Update(Student student)
{...}
[WebGet()]
public void Get(string Id)
{...}
}
Now the thing is that while updating a student record the client may not provide complete data. For e.g. it may provide Id and DOB but no name and LastName. Id being mandatory field. I need to know what could be the best approach/design in such a case?
I can fetch the existing record from db and perform a compare on both then update as necessary. The problem with this approach is that I have no way of knowing if a user actually wants to update a field to null. And then again, comparison does not seem a neat design. Any ideas?
After going through various strategies of handling this kind of thing I sort of implemented what was described in the answer to this question: Only update some properties on an EF entity that are not set to null.
In a nutshell I am tracking which properties were set during desrialization and then updating only those properties.