For future projects i decided to use dto's to pass data to the domain layer . Here is also where i make most of the data validation .
Where should i put data formatting ?
1) In the DTOs when it's ready to be sent to the domain layer
OR
2) In the infrastructure layer right before it's being persisted ?
OR
3) Somewhere else :)
i.e. : A password that needs to be encrypted before it's persisted or an image thats needs to be converted, resised etc before its stored.
I want to keep all data formating in one layer , don't like it to be scatered all over the place.
With other words : Should the data be prepared for the domain to handle it or should the domain get raw data and change it after it's being handled by the domain ?
Data formatting is a technical concern and as such it should be handled by infrastructural services, not by the domain. For example, password hashing should be handled by the repository which persists the corresponding aggregate. Formatting may also happen in a adapter in a hexagonal architecture which is where DTOs typically reside. This type of formatting is dependent on the type of adapter at hand. For example, you can consider a RESTful API exposing a domain model as being an adapter between HTTP and the domain model. In this case, formatting or translation must be performed between a resource representation and the corresponding domain objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With