Any tips for writing mapping code?
Currently at work I am writing mapping code. Code that takes values from one anemic object model to another anemic object model to another anemic object model. Aside from the usual iteration code, most of the code is comprised of this.setSomething(other.getSomething());. Unfortunately, this code does not map one-to-one from one model to another.
Being new to this kind of coding, I am wondering if anyone has any tips, tricks, or patterns. We are trying to avoid having a large (1000+ line) class filled with procedural, linear code. So we have a bunch of utility, finder and builder methods and classes. This has made something better, but I am wondering if we could do more.
There is a Java Bean Mapping framework named Dozer, which has a fairly rich feature set. See the post about it in the TheServerSide. I haven’t used it personally, but it definitely looks very promising.
For the target you control, if any, you could probably use annotations to indicate what member to call to get the value I’ve done similar stuff in C# using attributes and it usually turns out quite elegant.
Other than dozer, there are few libraries discussed at http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/
Thanks Yim, but I haven’t really discussed the libraries in detail, I’ve just looked at their performance.
Anyway, I wrote some test code that might help you to try some things out; you can downlaod the Eclipse project as well.
Cheers,
Christian
@Torbjörn - That would work, except we control no targets and are still using Java 1.4 so annotations are out (at least native Java ones) and the client is a long way away from letting us an annotations library.
@Alexei & @Carfield - Thanks for the recommendations, I will have to check them out.
The biggest problem with this mapping exercise is that we do not control any of the target domain models and one model uses all its own custom base object types (e.g., String, Decimal, Date, etc). Why any company would do this is beyond me.