Search code examples
c#restasp.net-coreentity-framework-corejson-patch

PATCH when working with DTO


I'm working on asp.net core webAPi and EF core, and want to implement "update" operation (partially edit entity). I searched the correct way to deal with that, and saw that I should use jsonPatch. the problem is that I'm expose just DTOs through my API, and if I use jsonPatch like:

public AccountDto Patch(int id, [FromBody]JsonPatchDocument<AccountDto> patch)

then I need to apply the patch on DTO, and I can't apply it on the model entity, without create a new entity.

I also read about Odata.Delta, but it still not work on asp.net core, and furthermore - I don't think it has a built in solution for working with dto (I found this example that can help when Odata for core will be available)

So, for now - should I use POST and send DTO with list of changed properties in query (as I saw here), Or - there is more elegant solution?

Thanks!


Solution

  • Now I saw that using autoMapper I can do just

    CreateMap<JsonPatchDocument<AccountDTO>, JsonPatchDocument<Account>>();
            CreateMap<Operation<AccountDTO>, Operation<Account>>();
    

    and it work like a charm :)