Answer the question
In order to leave comments, you need to log in
What are some good out-of-the-box solutions for implementing data transfer object in php?
Good time.
Historically, I work mainly with Laravel. But the Question is of interest, rather, as a whole. Not tied to a specific framework.
Some time ago I decided / there was a need to actively use DTO.
Useful to look for ready-made libraries.
I immediately noticed the Data transfer object from Spatie. It seems like it is considered the de facto standard in projects on the kiosk. Plus a lot of advice.
Yes, quite easy and simple. But.
I don't like that all the properties stick out (public). When filling the object through the constructor, we can still control what we shove there, but no one forbids us to assign any value to the property later. In general, the issue is solved by typing properties. Furthermore, each property value can be made to be an instance of the corresponding ValueObject. but still...
A quick note. I understand that the correct work with DTO is that in one place we fill the object with values, and in another we read these values. And nowhere along the way they should/can't be modified. If we still have the possibility that somewhere along the way from the source to the recipient, the data can change uncontrollably, then something is going wrong and I'm in big trouble. But paranoia refuses to accept rational reasoning.
Farther. Within this lib there is a concept of casters and validators.
Casters cast a value to some form before putting it into the DTO. And the validators. Well, they are validators in Africa too.
And everything seems to be fine, if not for the order of execution. Casters work first, and then validators. Which seems extremely strange to me.
For example, if we need to specify a port number, it is obvious that we first want to check if the number falls within the allowed range, and if the value is a number at all. And only then create a certain ValueObject with this value. But not vice versa.
In a word, Liba did not go.
And I wrote my solution based on Symfony OptionsResolver .
Works fine, performs its tasks, like the norms. But it does not leave the feeling that this is all from a cannon on sparrows.
In general, which one of you uses for these purposes?
Maybe there are good ready-made solutions that I have not seen.
Or should I just lower the bar.. I don’t even know the bar for what. Perfectionism, right? And just enjoy life?
Answer the question
In order to leave comments, you need to log in
I had a time when I wanted to do everything perfectly. And did. And then I looked at the result, it was possible to do it 10 times faster, because. functionality has not grown in several years.
And those things that I did flexibly and cumbersomely, and which often changed, are convenient. yes flexible.
But in fact, if everything was done in a simple, but wisely way. Those couple of components could actually be refactored.
In general, now I have switched to a model from small to large.
To make sure that the task was completed and the code was beautiful, but without overhead.
And suddenly DTOs became relevant, not a mess, and VOs became relevant, and not a bunch of code for no reason.
In general, refactoring as it becomes more complex, thus the level of entry into projects is much lower, and faster, and there is less code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question