Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DTO and mapper generation from Domain Objects

I have plenty of java domain objects that I need to transform to DTOs.

Please, don't start with the anti-pattern thing, the Domain Objects are what they are because of a long history, and I can't modify them (or not too much, see below).

So, of course, we've passed the age of doing all that manually. I've looked around, and dozer seems the framework of choice for DTO mapping.

But... what I'd really like is this: annotate classes and fields that I want in DTO, and run a tool that would generate the DTOs and the mappers.

Does that sound too unreasonable?

Does such a tool already exist?

like image 705
Nicolas C Avatar asked May 13 '10 16:05

Nicolas C


People also ask

What is the difference between DTO and Domain object?

If using anemic data model (i.e. your domain objects don't have any logic), DTO and domain object can be the same object. No. Domain objects have no specific relation to any persistence. In simple words, they are parts to ensure the business logic required to run the application.

What is DTO mapper?

DTO, which stands for Data Transfer Object, is a design pattern conceived to reduce the number of calls when working with remote interfaces. As Martin Fowler defines in his blog, the main reason for using a Data Transfer Object is to batch up what would be multiple remote calls into a single one.


2 Answers

Consider checking out ModelMapper.

It differs from Dozer and others in that it minimizes the amount of configuration needed by intelligently mapping object models. Where configuration is needed, ModelMapper offers a refactoring safe API that uses actual code to map properties and values rather than using string references or XML.

Check out the ModelMapper site for more info:

http://modelmapper.org

like image 140
Jonathan Avatar answered Sep 19 '22 16:09

Jonathan


You might be interested in MapStruct, a code generator for JavaBeans mappers. You'd have to implement source model (e.g. your domain objects) and target model (e.g. DTOs), and MapStruct generates type-safe and fast code for mapping between these models (disclaimer: I'm the author of this project).

like image 43
Gunnar Avatar answered Sep 17 '22 16:09

Gunnar