Automatic translation

Archives

April 2008
L My Me J V S D
"March May "
A 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

Contributors

Dozer: dare the mapping of beans!

That never faced having to write 10 lines of code just to copy the variables from one object to another?

Classic example of a MVC framework: You get the form submitted as Bean. You must then pass all values ​​to the object representing the model (model.setNom (bean.getNom ()), model.setAge (bean.getAge ()) ....).

This happens even when you have five values ​​to enter, but with forms containing 20 fields and that's another story ...

To simplify all this, there is a small framework that requires only one line of code: Dozer

MapperIF mapper = new DozerBeanMapper();
DestinationObject destObject = (DestinationObject) mapper.map(sourceObject, DestinationObject.class);

That's it. This requires however that the attributes of the two objects have the same name. Otherwise we must add an XML file to describe the various fields to map:

<mapping>
<class-a>package.SourceClassName</class-a>
<class-b>package.DestinationClassName</class-b>
<field>
<A>SourceFieldName</A>
<B>DestinationFieldName</B>
</field>
</mapping>

In this example: the attribute of the object SourceFieldName SourceClassName will be copied to the attribute of the object DestinationFieldName DestinationFieldName. It is also possible to specify the attributes to be excluded when copying.

In short, this is a very useful tool to offload tedious sometimes inserting the values ​​of a Bean to another object.

  • Bertrand http://www.ippon.fr

    We already knew well BeanUtils (thank you AppFuse!). But that framework is going one step further by allowing you to specify mappings!
    It can indeed be handy.

  • Fabien http://www.ippon.fr

    Indeed this framework has potential.

    To use a bit more advanced, I note in particular the ability to specify a custom converter in each field of a mapping: http://dozer.sourceforge.net/documentation/custom ...
    Indeed, a recurring problem when we want to map two models is to convert the field values ​​using lists of values.

    Using an IoC container and method:
    public void DozerBeanMapper.setCustomConvertersWithId (java.util.Map customConvertersWithId)
    will surely inject easily converters capable of handling transcoding based on a table stored in the BDD.

    As part of an exhibition Web Service, where the management of mappings between lists and tables often necessary (if not using Generics), Dozer also seems to have anticipated this need: http://dozer.sourceforge .net / documentation / collections ...

    A single black spot in my opinion: it does not seem possible to specify the mapping configuration other than via an XML file loaded from the classpath ...
    This severely limits the possibilities of configuration.
    => This makes it impossible to define such mapping via a DSL.

  • Lawrence Craecker

    With Joda DateTime type fields, does it work well?