Using dozer context mapping as factory bean

If you have a DTO (Data transfer object) or a VO (Value Object) and you want to mapping them
to a specific Pojo depending of a value of an attribute, you must to implement with a multiple if/else if
condition or instance of.

For example we have CarRaceVO with type attribute. If type have “F1″ value we instantiate
an F1Pojo, if type == “Nascar”, a NascarPojo etc.

We don’t want to write pieces of code like this:



if ("F1".equals(carRaceVO.getType()))

 // instantiate a F1Pojo

else if ("NASCAR".equals(carRaceVO.getType()))

 // instantiate a NascarPojo

else if ("IRL".equals(carRaceVO.getType()))

 // instantiate a IrlPojo

Why not we use Dozer and his Context Conversion System?

It’s quite simple.
First: create some context in the dozer mapping xml configurations.




<mapping map-id=”F1″>
    <class-a>com.dozer.tutorial.CarRaceVO</class-a>
    <class-b>com.dozer.tutorial.F1Pojo</class-b>
   <field>
    […]
   </field>
</mapping>
<mapping map-id=”Nascar”>
     <class-a>com.dozer.tutorial.CarRaceVO</class-a>
     <class-b>com.dozer.tutorial.NascarPojo</class-b>
     <field>
      […]
     </field>
</mapping>


     

Then, you can select the correct mapping retrieving the value of the property type
of CarRaceVO class.You must specify your mapper with this simple line code:



ContextMappingPrime cmpA = (ContextMappingPrime) mapper.map(cm,
            ContextMappingPrime.class,
            carRaceVO.getType());

2 Responses to “Using dozer context mapping as factory bean”

  1. of world poker 2004 series series poker main of world

  2. for evista evista download

Leave a Reply