S
S
Sergey Ilichev2021-04-08 23:59:24
PHP
Sergey Ilichev, 2021-04-08 23:59:24

Is it good to use DTO as request object for api?

Good evening colleagues!

It's not entirely clear from the title what I mean, so I'll explain it here.

During the refactoring of our microservices, I was advised not to use a bunch of parameters for the method in the api, but simply to pass a DTO object there, they say this unifies requests to the api. It seems interesting, but I don’t really understand the meaning (right now at night you can’t ask a colleague anymore). Firstly, it is not clear how, based on this, other developers should understand what needs to be transferred there. Well, yes, let's say this is our microservice and I'll create a DTO in microservice A and send it to the method of microservice B, like I can see what fields are described in the DTO, set them and send the object. But what if the developer does not know anything about this DTO? He just sees that the method of service B accepts some kind of DTO, and you won’t understand what should be in it without looking at the code.

I'm not particularly familiar with Symphony yet, but a colleague wrote to me that you can use a type of param converter for these purposes. I began to google what it is and found this expression only in relation to the symphony, and as I understand it, this cannot be transferred to Yii, everything needs to be done with handles, that is, create these DTOs. And even if all the same it would be possible, I honestly do not see a profit.

If anyone understands what a colleague meant and why it is generally considered convenient, please tell us.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim, 2021-04-09
@first-programmer

It is difficult to understand your picture and what your colleague meant. It is better to ask a colleague about this. However, I will try to think about this topic...
1. Specifically, DTO is not entirely applicable to the API in its pure form and work with it. It is more correct to throw json into the request, but this json is already mapped to your DTO, in which there will be high-level validation from symphony and php7.4. Example And the DTO itself .
2. Since you have a microservice system, you definitely don’t need to transfer DTO over the network. You can use DTO inside your application, but not use it over the network. DTO allows you to pass to the service / handler / command not the Request itself and not a bunch of parameters, but your own DTO, which should be created based on the request. This way you separate layers and responsibilities. When changing the names of DTO parameters, it is not necessary to change them in the API and vice versa. No addiction and that's good! Just as good for testability.
3. If you mentioned ParamConverter, this is essentially a parameter convector, but again at the application level, and not between network requests) You can read about its use in the symphony documentation. With it, you can specify in the method the DTO that it accepts, and the convector written by you knows how to create this DTO based on your transmitted data. That is, you do not pass DTOs to the request, but the same parameters, this convector simply makes DTOs out of these parameters for you. Whether to use it or not is up to you. You correctly noted that there is no such thing in Yii2. You can get confused to write your own, but it makes no sense at all) I would recommend using DTO. Since the profit will be: separation of layers, less connectivity and the possibility of validation. But to use a convector is the decision of the team or the developer. It allows you to create your DTO based on the passed parameters. Parameters will not go anywhere, they will simply fall into DTO right away, this is one profit - cleaner code.

D
Daria Motorina, 2021-04-09
@glaphire

DTO simply helps to get rid of direct work with the entity / model and take out all the preliminary initialization / processing / validation there, it turns out as an additional layer of data transfer responsibility, so as not to solve it at the level of the same models / entities or services.
With DTOs on different sides of two microservices, the question is complicated)
ParamConverter (and ArgumentResolver) are additional layers in the symphony request life cycle, they are simply resolvers of raw request parameters and their validation / conversion into objects before entering the controller action. Judging by the documentation, there is no such functionality in Yii2 and you really need to do it yourself or simplify it.

D
Dmitry Gordinskiy, 2021-04-09
@DmitriyGordinskiy

In addition to the above, the set of parameters passed to the public method of the service often continues to walk through private methods or service components, and it is easier to do this with one DTO than with a bunch of parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question