目前有一个asp.net web api和使用AspNetCore.JsonPatch
实现JSON补丁,并希望执行一个副作用,如果一个动作是通过JSON补丁完成?
。更新数组的补丁请求完成后,会触发另一个函数被调用来更新数据库。
有什么内置的东西来做这个吗?
没有内置的可以做到这一点,但您可以使用automapper
来实现它。参考这个演示:
[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;
}