Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where,when and how convert DTO to/from Entity

I have project in which i have

  • A persistence layer
  • Business layer
  • Presentation layer

and also I have DTO for every Entity,

@Entity
@Table(name = "insurance_config")
public class InsuranceConfiguration {

and DTO

public class InsuranceConfigurationDTO {

from architecture perspective what is the best practice to convert DTO to/from Entity ?

In which layer the conversion should take place?

Should I put the conversion methods inside the DTO/Entity or in separate class ?

like image 860
Melad Basilius Avatar asked Sep 06 '25 03:09

Melad Basilius


2 Answers

You should introduce interface layers between the web/service/persistence layer and avoid transitive dependencies. And the transformation logic should not be included in the DTO's rather in a different class, but that's my opinion.

For better understanding, I created a simple UML for this:Might not the best UML in the history of UML diagrams

(PS.: I could post this project to github if needed)

like image 115
Tacsiazuma Avatar answered Sep 07 '25 22:09

Tacsiazuma


here is a link where you get to know about the TOA design pattern. I think this is what you looking for. Here you call a dao class and after you get an object or list of object you can call a Dozer mapper which you can use for convert from entity to dto and it is may be in util package or mapper package. I think the best is the Business layer or may be the Persistence...but of course not the Presentation..

like image 30
kodaek98 Avatar answered Sep 07 '25 22:09

kodaek98