Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate DTOs from Object

I want to auto gen some DTOs from some of my existing objects, I was wondering if there already exist a Resharper, DevExpress or VSX open source to use, I need a tool to look into my code and let me choose which properties I want to include in my DTO and then generate a class based on that...

like image 376
Ali Shafai Avatar asked May 07 '09 01:05

Ali Shafai


People also ask

Are DTOs value objects?

Value objects are sometimes confused with DTOs, however, while DTOs are simple data containers with no significant logic, Value Objects are a “thing”, they represent a cohesive concept; they can wrap multiple attributes and the logic associated with them to fully encapsulate that single concept.

Can DTOs have methods?

DTO doesn't have methods. All of the methods that do calculations based on members of DTO are in the dependent class. The more DTOs the dependent class depends on, the more private methods the class has. The more private methods the class has, the harder it is to understand code and write unit tests.

What is DTO object in Java?

In the field of programming a data transfer object (DTO) is an object that carries data between processes. The motivation for its use is that communication between processes is usually done resorting to remote interfaces (e.g. web services), where each call is an expensive operation.


2 Answers

Another opinion would be to use some reflection code in a T4 template.

T4 templates are one of the powerful hidden features of Visual Studio.

Take a look here to see how easy it is to get started with T4.

like image 134
Alex James Avatar answered Nov 03 '22 00:11

Alex James


This is kind of a hack but you could use Visual Studio/Resharper to Extract an interface from the class, then create a DTO class and derive from the interface, then use Visual Studio to auto implement the interface members (then, optionally remove the interface).

like image 37
NotDan Avatar answered Nov 03 '22 00:11

NotDan