code generator for creating DTOs and mapping to domain objects

Project maintained by stephenh Hosted on GitHub Pages — Theme by mattgraham

Automatic DTO Generation and Mapping

dtonator automates the tedious aspect of maintaining a DTO layer for exposing domain objects over the wire.

This is handy for building REST or AJAX interfaces, where the JSON or XML you send over the wire is generated for you, but not directly from your domain objects, but instead a set of custom DTOs (so that the REST/AJAX interface can be different than your domain objects).

Currently, I use dtonator for generating GWT-RPC compatible DTOs, and also generating client-side models for use in Tessell; however, it is not tied to either of those technologies.

(For GWT users, dtonator, combined with a dispatch-style framework, is my take on a simpler, more straight forward Request Factory.)


Here is a brief screencast of dtonator’s workflow:

Code Examples

For example, given a dtonator.yaml file like:

  dtoPackage: com.bizo.dtonator.dtos
  domainPackage: com.bizo.dtonator.domain
  mapperPackage: com.bizo.dtonator.mapper

  domain: Employee

dtonator will generate an EmployeeDto class, with all of the primitive fields in Employee. You can then use the generated Mapper to go back/forth between your domain and the DTO, like:

public void testMapper() {
  // Mapper is generated for you
  Mapper m = new Mapper(...);
  Employee e = findSomeEmployee();
  EmployeeDto d = m.toDto(e);
  // send d over the wire...

  // later get d back over the wire
  e = m.fromDto(d);

dtonator is also smart enough to map DTOs back onto the correct domain object, via the DomainObjectLookup interface:

public interface DomainObjectLookup {
  <T> void lookup(Class<T> domainType, Long id);

You provide dtonator with an implementation of this interface, which uses your persistence framework (Hibernate, Joist, etc.) to find the domain objects, which dtonator will then write the DTO values back in to.

(Obviously your business logic should verify the user is accessing only the domain objects they’re allowed to.)

Other Features


Feedback, changes, etc., feel free to contact me via github/pull requests/etc.