22

I am trying to figure out an easy way to map DTOs to entities without the boiler-plate code. While I was thinking of using dozer it appears to require a lot of xml configuration. Has anybody seen a dozer alternative that uses a DSL to configure the bean mapping in pure Java?

Ideally I am hoping to find a bean mapper that is inspired by the way Guice does things.

benstpierre
  • 32,833
  • 51
  • 177
  • 288
  • Another option would be http://jtransfo.org/. This allows mappings to be defined using annotations on the TO. It has the additional feature that you can have security/optional conversions based on tags. – Joachim Van der Auwera Jul 08 '13 at 09:37
  • 1
    take a look at https://github.com/amgohan/zebra/ : (DIY objects mapping and use zebra for unified way to inject mappers.) Zebra come with a simple and unified way to : create your mappers; manage one way and reverse mapping in one class; register your mappers and reuse them anywhere in your application; manage deep mapping. – amgohan May 29 '15 at 12:56
  • This is a pretty good alternative: http://modelmapper.org/ – cosbor11 Aug 20 '15 at 02:57
  • 1
    Now (since version 5.3.2) Dozer supports [annotation mappings](http://dozer.sourceforge.net/documentation/annotations.html). Additional mappings, which can not be derived by the naming (implicitly), can be added either via Xml, Annotations or API. – naXa stands with Ukraine Jan 19 '17 at 12:10

3 Answers3

16

Look at Orika.

Orika is a Java Bean mapping framework that recursively copies (among other capabilities) data from one object to another. It can be very useful when developing multi-layered applications.

Sidi
  • 1,739
  • 14
  • 16
  • 1
    Orika did the job for me, handled collection mapping case without much effort. Very powerful and usable library IMO. – Sumit Jain Jan 04 '17 at 06:50
11

I was looking for alternatives as well.

Here is a very good coverage of different options.

Community
  • 1
  • 1
Gennady Shumakher
  • 5,637
  • 12
  • 40
  • 45
4

From my point of view, the configuration or java code to map some special properties are always needed.

Here I want to take a DO and DTO for example

DO:{
    id: "id",
    name:"name",
    doName1: "doName1",
    nestedObj: {
        id: "nestedObjId",
        name: "nestedObjName"
    }
}

DTO{
    id: "",
    name: "",
    name1: ""   // for mapping doName1 in DO.
    nestedId: "", //for DT.nestObj.id
    nestedName: "", //for DT.nestObj.name
}

For Dozer or Orika they both can automatically match id and name property between DO and DTO without any configuration or java code because they are with the same property names and types. But if you want to DO.doName1 <----> DTO.name1 or DO.nestedObj.id <--->DTO.nestedId you need to make some configuration (via xml or java) to tell the mapping tool you are intend to do that. I think for your use case, Dozer, Orika and ModelMapper are all OK. But for me I am switching my project from dozer to Orika for performance purpose. Although Orika is not so mature as dozer, not so intelligence and requires me to do a lot additional job to maintain my customized mapping configurations. If your project not too care about performance I will recommend you dozer, it’s so easy to use and support so many advanced features. Otherwise if you are purchasing high performance, i suggest you orika.

Bruce Wang
  • 41
  • 4