Scenario: I'm having user object details saved in database which contains information related to user. These information can be Personal details (Name, Email), Authentication details (Dob, Employee Id), Additional information (Address, Gender, Cellphone) and Authorization details (what access permissions that user have).
Requirement: 1) I want to expose restful endpoint (using MVC4 Web Api) which will update these information. User can even clear off the existing details also. 2) I want the consumer of that endpoint to pass only changed/updated values. For ex- if user wants to update Cell Phone, then no need to pass other details
Issues: 1) MVC4 framwork automatically desrializes Json into Api parameter class object. So, the fields which are not passed in Json will have null value after deserialization. I need to differentiate between the fields which were not passed by consumer AND the fields which were passed by user and those value needs to be clear off. 2) Is it good to expose different endpoints to update different sections like personal information or Additional information or expose only one endpoint? If I expose different endpoints, then consumer will have headache of firing more than one endpoint to update the information for same user.
Possible approaches to handle Issue #1: 1) User will pass only those fields which needs to be updated. So, expose single endpoint say Edit. Define a parameter class having all user details field type to be string. So, if user wants to remove any field data, then it has to pass as "" (empty string). In that way, I will be able to differentiate between fields not passed and fields to be remove.
// UserController.cs
public HttpResponseMessage Edit(UserDetails incomingParameters)
{
// This is just pseudo code.
// Get existing user details.
UserObject existingUserDetails = GetExistingUserObject(string username);
// Now, convert put the non-null fields in incomingParameters into
// existingUserDetails. No need to put null values in incomingParameters
// as those were not passed by consumer. I can implement extension
// method here.
existingUserDetails = incomingParameters.ToUserObject();
// After that, my object to update is ready. Pass this
// existingUserDetails to ORM and it will update into database.
}
// UserDetails.cs
public class UserDetails
{
public string Dob { get; set; }
public string CellPhone { get; set; }
public string EmployeeId { get; set; }
public string Gender { get; set; }
public string Street { get; set; }
public string Email { get; set; }
}
// UserObject.cs
public UserObject
{
public DateTime? Dob { get; set; }
public string CellPhone { get; set; }
public string EmployeeId { get; set; }
public Enum.UserGender Gender { get; set; }
public string Street { get; set; }
public string Email { get; set; }
}
2) Implement custom deserializer onActionExecuting and look for fields which are passed by users.
Note: It will secured endpoint. So, I don't need to worry about security issues.
Aucun commentaire:
Enregistrer un commentaire