DDD - which DTO layer should be implemented

I tend to DDD, so I apologize if my question is naive. I think I need to use a local data transfer object to display data for users, since many properties are not part of any Entity / Value object.

However, I'm not sure where this DTO should be implemented - at the domain level or at the application service level. The DTO implementation seems to be part of the Domain, but that means that when I create the DTO collection at the Service Level and pass it to the presentation level, I have to refer to the domain level at the presentation level, which seems wrong.

What is the correct way to implement DTO using DDD principles?

+18
source share
3 answers

Place it at the domain service level . DTO is the output of this layer, it makes sense if you define it there.

Do not put your DTO at the domain level. A domain layer does not care about things outside its domain.

UPDATE: the domain service level is commonly known as the "application service" level

+16
source

Yorro is right where to place the DTO, but I urge you to avoid "thinking DTO." This way of thinking interferes with DDD thinking.

The thought of β€œI need a DTO here” is thinking of a technical presentation (as plalx says); this level of abstraction is too low. Try a higher level of abstraction and think about your domain, user tasks, and user interface.

Need to get viewing data for a user? Connect it to the user interface through a view service that returns a specific YourViewInfo class.

Need to send data to a service to complete a task? Send it a specific TaskMessageInfo class or a specific class of commands.

When you start modeling the interiors of these classes, you should start thinking about your technical background; then you can come to the conclusion, which may be, for example, DTO classes for convenience.

Thinking in this way helps to model the system and does not raise questions such as

Where to put this thing?

+8
source

DTO and Domain are different layers.
Therefore, mapping one to the other is required, and usually this is done at the so-called application service level.
Take a look at the following articles to delve deeper into the DTO and layered structure:

+8
source

All Articles