1

Topic: How correctly to do DTO?

Under DTO I understand classes (structures) dataful, without any essential logic. For example such classes are accepted/returned often by an access layer to a DB or with them REST-controlers, web services and  operate. The first variant it bluntly structure with fields. In world Java to it it is normal  trivial gettery-setters, in other worlds do  or simply put out fields outside, an essence one. What I see minuses: at first often there are some requests which return different number of the data. We tell one business it to pull out essence on ID, here it is possible and  other entities, another matter it to do difficult , returning one thousand entities, any  here is unpleasant, and it would not be desirable to pull simply superfluous field, if it is not necessary. I.e. Requests as a matter of fact return different types, but logically at them a heap of the general fields and to do different classes on each request it of strange, one names will not save enough on such classes, besides quite often the code works with all these classes and in a typed language it is necessary to invent any hell with interfaces. Easier simply to make one megaclass with all fields well and each request fills their some subset. As a result each field can accept value undefined which in the same Java directly does not express. If strongly without philosophizing to use null it is easy to admit a bug when the code thinks that this field is derived from basis, and it is not derived, the type system does not help and it is good, there will be NPE, and even simply error in the logician. JavaScript theoretically here it is better, each value can be undefined though in practice with such values it can be uneasy to work correctly. The second variant it to make for each field of value undefined, and such undefined that at any attempt it to use the error takes off. Or to make a class-holder, or in special way to generate DTO-classes. Such approach is pleasant to me most of all, but it is heavy for using: the naive approach conducts to the big over-expenditure of storage, excessive loading on GC and to additional passages under links, at least in Java, and the competent approach demands special library which not so is trivial to write. In passing there is one more problem. Often by operation from a DB fasten . On the other hand if we have a class with "setters" it is possible to receive  value, to cause on it a setter (unintentionally) and to forget about it. Next time at us same  value already will be with the changed field. That is it would be desirable to give often  objects. On the other hand the object of the same class nearby should be , received value, changed, saved in a DB. To do all objects , in my opinion, it is search. Therefore it would be desirable besides  to store for its each field . Well and too it is effective, finite, instead of to produce . Well and for an ORM-like functional it would be desirable change tracking that is that update saved only the changed fields. Any not absolutely simple DTO-object as a result turns out. How these problems dare? I will be especially grateful to the information about Java, but also for other languages interesting. In the same Java while anywhere did not see normal decisions, in any way these problems normally are not solved, hands are therefore scratched to write the "" with megaoptimal generation .

2

Re: How correctly to do DTO?

Hello, vsb, you wrote: vsb> Under DTO I understand classes (structures) dataful, without any essential logic. For example such classes are accepted/returned often by an access layer to a DB or with them REST-controlers, web services and  operate. vsb> the First variant it bluntly structure with fields. In world Java to it it is normal  trivial gettery-setters, in other worlds do  or simply put out fields outside, an essence one. vsb> what I see minuses: at first often there are some requests which return different number of the data. We tell one business it to pull out essence on ID, here it is possible and  other entities, another matter it to do difficult , returning one thousand entities, any  here is unpleasant, and it would not be desirable to pull simply superfluous field, if it is not necessary. I.e. Requests as a matter of fact return different types, but logically at them a heap of the general fields and to do different classes on each request it of strange, one names will not save enough on such classes, besides quite often the code works with all these classes and in a typed language it is necessary to invent any hell with interfaces. Easier simply to make one megaclass with all fields well and each request fills their some subset. As a result each field can accept value undefined which in the same Java directly does not express. If strongly without philosophizing to use null it is easy to admit a bug when the code thinks that this field is derived from basis, and it is not derived, the type system does not help and it is good, there will be NPE, and even simply error in the logician. JavaScript theoretically here it is better, each value can be undefined though in practice with such values it can be uneasy to work correctly. DTO this glue, it is not necessary to subtilize here even if something repeats it not so terribly. If there are no names to use name spaces, for example: Service1.Dto, Service2.Dto etc. Well it would be possible to use inheritance what to reuse such fields as for example Id not much. If all it does not approach, it is possible to transfer what be dictionary with key-value, only it is already curve method. vsb> the second variant it to make for each field of value undefined, and such undefined that at any attempt it to use the error takes off. Or to make a class-holder, or in special way to generate DTO-classes. Such approach is pleasant to me most of all, but it is heavy for using: the naive approach conducts to the big over-expenditure of storage, excessive loading on GC and to additional passages under links, at least in Java, and the competent approach demands special library which not so is trivial to write. vsb> in passing there is one more problem. Often by operation from a DB fasten . On the other hand if we have a class with "setters" it is possible to receive  value, to cause on it a setter (unintentionally) and to forget about it. Next time at us same  value already will be with the changed field. That is it would be desirable to give often  objects. On the other hand the object of the same class nearby should be , received value, changed, saved in a DB. To do all objects , in my opinion, it is search. Therefore it would be desirable besides  to store for its each field . Well and too it is effective, finite, instead of to produce . vsb> Well and for an ORM-like functional it would be desirable change tracking that is that update saved only the changed fields. vsb> any not absolutely simple DTO-object as a result turns out. Well, if still to add the postponed loading of the data that will be at all DTO, and D. There are patterns for this purpose, for example the active record. vsb> As these problems dare? I will be especially grateful to the information about Java, but also for other languages interesting. In the same Java while anywhere did not see normal decisions, in any way these problems normally are not solved, hands are therefore scratched to write the "" with megaoptimal generation .

3

Re: How correctly to do DTO?

Hello, Qulac, you wrote: Q> DTO this glue, it is not necessary to subtilize here even if something repeats it not so terribly. If there are no names to use name spaces, for example: Service1.Dto, Service2.Dto etc. class GetPersonById. Record {string lastName, string firstName, string patronymic, Address address}, GetPersonListByCountry. Record {string lastName, string firstName, string patronymic}? Q> Well it would be possible to use inheritance what to reuse such fields as for example Id not much. If all it does not approach, it is possible to transfer what be dictionary with key-value, only it is already curve method. Well here an example above. At me a method which returns  for the person. getPersonFullName (??? person) {return person.firstName + "" + person.lastName + "" + person.patronymic}. And what here type to transfer? vsb>> Well and for an ORM-like functional it would be desirable change tracking that is that update saved only the changed fields. vsb>> any not absolutely simple DTO-object as a result turns out. Q> well if still to add the postponed loading of the data that will be at all DTO, and D. There are patterns for this purpose, for example the active record. Not, the postponed loading of the data it angrily, such is not necessary for nothing. The data should be loaded in one place and . Well, or, at least, it should be a rare exception when without it in any way, for the sake of it it is possible to write also pens all. Here speech about tracking of changes when the programmer loads object from basis, changes there any fields and saves object in basis, and the library code defines, what it fields changed and does update this field.