Question

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.

Was it helpful?

Solution

I was looking for alternatives as well.

Here is a very good coverage of different options.

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top