B
B
BonBon Slick2018-05-15 22:27:50
Software design
BonBon Slick, 2018-05-15 22:27:50

Where, when, why and why should DTO be used?

Actually, everything is written in the question. DTO is only data typing, but it would be easier to operate. So when, where, when, why and why?
Why I mean why apply it in a particular situation. Let's tell whether it is possible to transfer UserDto with all parameters to the user's constructor? Or is it not possible? Why? Methods? Where are the boundaries, I doubt that DTO can be applied to all methods on all layers of the application. and in general I doubt that everywhere it is possible and necessary. And another question, is the factory needed only for entities, aggregates? Or factories also need to be created for everything that is often initialized in the project?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2018-05-16
@BonBonSlick

1. DTO models are just a layer between services and their purpose is to transfer data from one service / layer to another. These are always pure POCO models. If you immediately transfer data from one service to another service, then you will become dependent on the type of data. That is, if the type of returned data changes in the first service, then you will have to change the input data type of the second service and perhaps even edit something in the code.
A layer of DTO models protects against this. If the type of returned data changes in the first service, then you simply change the rules for mapping the returned data to the DTO model and you don’t have to edit anything in the second service. The same applies in the opposite direction. Well, plus, from the point of view of philosophy, it allows you to work more efficiently with models at the level of the subject area of ​​the service itself, not to depend on the subject area models of a third-party service.
Of course, the DTO layer adds complexity to the project. Firstly, an extra layer, extra classes, and secondly, we need mapping rules from one service to DTO and from DTO to the second service. Therefore, all this is justified on large long-lived projects with a high expectation of changes.
Actually, regarding the specifics of the application, I answered: before giving the data - map it to DTO, if you received DTO - immediately map it to local models. You don’t use them anywhere else inside, they only transfer data.
2. A factory is needed where control over the creation of an object is needed. Either limiting the rights to create it, or making a decision to create it by condition, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question