Where, when, and why it is necessary to use DTO?

Actually the question says it all. DTO only typing, but easier to operate. So when, where, when, and why?

Why I mean why use it in a particular situation. say is it possible in the constructor of UserDto user to pass all parameters? Or it is wrong? Why?Methods? Where what are the boundaries, I doubt that all the methods in all layers of the application, you can apply the ATT. anyway I doubt that anywhere can and should be. And another question, the factory is only needed for entities, units? Or factory so it is necessary to create all that often is initialized in the project?
June 5th 19 at 21:48
1 answer
June 5th 19 at 21:50
Solution
1. The DTO model, this is just a layer between services and their purpose to migrate data from one service layer to another. It is always clean POCO models. If You from one service will immediately transmit the data to another service, then You will be dependent on the data type. Ie, if the first service will change the type of the given data, you will have to change the input type of the second service and possibly even edit in code.

A layer of DTO models from this protects. If the first service will change the type of the given data, You just change the rules of mapping given data to the DTO model in the second service there is nothing to rule not necessary. The same goes in the opposite direction. Well, plus, from the point of view of philosophy, you can better work with the models at the level of the subject area of the service, do not depend on models of the subject area of third-party service.

Of course the DTO layer adds complexity to the project. First extra layer, the extra classes needed in the second rule of mapping from one service to DTO and from DTO to the second service. So it's all justified for large long lived projects with great expectation changes.

Actually, regarding the specifics of the application, I replied, before sending data - mapia in DTO, received DTO from mapate in the local model. More inside anywhere not using them, they only transfer data.

2. Factory is required where desired control object creation. Or restriction of the rights of its creation or the decision of creating the condition, etc.

Find more questions by tags Patterns of designingDesigning software