måndag 19 januari 2009

Nice util to copy properties!

Have you ever used for instance JPA over EJB and got frustrated when you manually need to set your getters and setters of the DTO manually? I certainly have and in my current project we developed our own tool to handle these kind of problems.

Database (entity obejct) <-> DTO (transfer object) <-> EJB cloud <-> UI that uses DTO.

This is probably a common way to implement an application that takes advantage of Javas EJB but it also means A LOT of set and get between the DTO and the database entitys when you need to transfer data from backend to frontend.

Our first solution was a simple one, we used reflections to find getters and setter and then just transferred the values. However problem arised when we had lists with complex types i.e. DTO's in DTO's. Our copy properties class util just grow bigger and the performance was terrible!

One day we found this lovely tool on sourceforge called Dozer, Dozer homepage. Out of the box it handles all of these problems without any problem and it perform great! I made a quick benchmark with 10000 entitys and found it 8.6 times faster than our own implentation :)

It works like this. Lets say we have our entity object DogEO and our transfer object DogDTO:



DogDTO dto = new DogDTO();
dto.setHeight = 2;
dto.setWeigth = 23;

DogEO dogEo = new DogEO();

MapperIF mapper = new DozerBeanMapper();
dogEo = (DogEO) mapper.map(dto, DogEO.class);



This maybe seems like a small problem but trust me when you have 30+ DTO's with complex structure and content that are subject to constant change, then this is the shit!

Inga kommentarer: