Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD : Where should data be converted, formatted, encrypted etc?

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 ?

like image 557
Tudor Avatar asked Oct 05 '12 21:10

Tudor


1 Answers

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.

like image 160
eulerfx Avatar answered Nov 08 '22 19:11

eulerfx