'Perform side effect after Json Patch
Currently have an asp.net web api and implementing JSON Patch using AspNetCore.JsonPatch and want to perform a side effect if a certain action is done via Json patch?
E.g. patch request to update an array is done then triggers another function to be called to update a database.
Is there anything built in to do this?
Solution 1:[1]
There is no built in can do it, But you can use automapper to achieve it. Refer to this demo:
[HttpPatch("update/{id}")]
public Person Patch(int id, [FromBody]JsonPatchDocument<PersonDTO> personPatch)
{
// Get our original person object from the database.
PersonDatabase personDatabase = dbcontext.xx.GetById(id);
//Use Automapper to map that to our DTO object.
PersonDTO personDTO = _mapper.Map<PersonDTO>(personDatabase);
//Apply the patch to that DTO.
personPatch.ApplyTo(personDTO);
//Use automapper to map the DTO back ontop of the database object.
_mapper.Map(personDTO, personDatabase);
//Update our person in the database.
dbcontext.xx.Update(personDatabase);
return personDTO;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Xinran Shen |
