DDD: where should the data be converted, formatted, encrypted, etc.?

For future projects, I decided to use dto to transfer data to the domain level. Here I also do most of the data validation.

Where should I put the data formatting?

1) In DTO, when it is ready to be sent to the domain level
OR
2) At the infrastructure level before its preservation?
OR
3) Somewhere else :)

i.e. .: Password that must be encrypted before it is saved, or an image that needs to be converted, changed, etc. before saving it.

I want all the data to be formed in one layer, I do not like it to flow around everywhere.

In other words: should the data be prepared so that the domain processes it, or should the domain receive raw data and modify it after it is processed by the domain?

+4
source share
1 answer

Data formatting is a technical concern, and therefore it should be handled by infrastructure services, not a domain. For example, password hashing should be handled by the repository, which is stored in the appropriate aggregate. Formatting can also occur in the adapter in the hexagonal architecture where the DTOs are usually located. This type of formatting depends on the type of adapter at hand. For example, you might consider a RESTful API demonstrating a domain model as an adapter between HTTP and a domain model. In this case, formatting or translation must be performed between the presentation of resources and the corresponding domain objects.

+5
source

Source: https://habr.com/ru/post/1438094/


All Articles