Melad Basilius
Melad Basilius

Reputation: 4306

where,when and how convert DTO to/from Entity

I have project in which i have

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 ?

Upvotes: 4

Views: 3910

Answers (3)

Alex
Alex

Reputation: 2335

I took the liberty of implementing Tacsiazuma's design, it's here

Upvotes: 0

Tacsiazuma
Tacsiazuma

Reputation: 776

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)

Upvotes: 7

kodaek98
kodaek98

Reputation: 473

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..

Upvotes: 2

Related Questions