Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 782
arielorvits Avatar asked Jul 19 '16 20:07

arielorvits


People also ask

What is the use of a DTO?

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.

What is DTO pattern in Java?

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.

Why is jsonpatchdocument<t> used instead of a DTO?

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.

What is a data transfer object (DTO)?

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.


1 Answers

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 :)

like image 110
arielorvits Avatar answered Oct 12 '22 14:10

arielorvits