Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to translate from DTO to Entity and Entity to DTO?

I have worked on several distributed client/server projects recently, and one pain point that we always run into is translating the DTO objects into our entities and vice-versa. I was wondering if anyone has a "simple" solution to this time sink?

One thing I thought about was coming up with some sort of translation using reflection...I guess you'd have to make sure your property names were exactly the same on each side of the wire - but seems like it might work.

Just looking for a way to avoid some of this time sink in my development.

Thanks!!

like image 638
Steve Horn Avatar asked Oct 15 '08 13:10

Steve Horn


People also ask

Why do we convert entity to DTO?

Entities represent the persistence model of an application and they are always internal to the application. On the other hand, DTOs (Data Transfer Objects) represent the models which the application exposes with the outside world.

Can we use entity as DTO?

IF the entity and DTO are exactly the same, and IF you can guarantee that there will never be any changes to the data model; then you are correct that you can omit the DTO. But the thing is that you can never guarantee that the data model will never change.


2 Answers

I have been using AutoMapper recently, and it works like a charm.

like image 178
CitizenBane Avatar answered Oct 18 '22 14:10

CitizenBane


Pain and risk

of getting/setting properties by hand! Have you tried this: http://dozer.sourceforge.net/.

A flexible and configurable framework for translating bean to bean. Might help!

like image 35
paulosuzart Avatar answered Oct 18 '22 16:10

paulosuzart