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!
The DTO is mainly used for reducing the number of expensive remote calls. In order to convert data between the DTO and any entity objects, the assembler object was defined, but now we are using mappers for converting data.
DTOs or Data Transfer Objects are objects that carry data between processes in order to reduce the number of methods calls. The pattern was first introduced by Martin Fowler in his book EAA. He explained that the pattern's main purpose is to reduce roundtrips to the server by batching up multiple parameters in a single call.
For that reason it uses something called JsonPatchDocument<T> on top of the DTO you use for updating the entity. Unlike the plain DTO JsonPatchDocument contains some instructions on how you want to update the entity.
DTO stands for Data Transfer Object, which is a design pattern. It is one of the EPA patterns which we call when we need to use such objects that encapsulate and aggregate data for transfer. A DTO is similar to a data structure, but like a data structure, it doesn't contain any business logic.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With