One of the reasons this blog has been a bit quiet is that I have been working furiously on Madura Objects. Now I have uploaded it to Github for all the world to see (if it wants).
What is it? Imagine you have a bunch of Java objects that represent real things like Customer, Order, Address, Product etc. We spend a lot of time plumbing these kinds of objects in our applications. Typically we write business logic to manage them, validate them, persist them etc. Using JAXB we have a way to serialise Java objects to and from XML, and I use this a lot when building web services.
Thanks to Hyperjaxb3 we can also take these same objects and persist them to Hibernate (and other places which I have not yet tried). This all simplifies the plumbing logic hugely. But I wanted to do a little more and the JAXB approach easily lends itself to transparently adding more ‘stuff’ to the Java objects.
I’ll explain that a little more. You actually define your objects in XSD, pass them through the JAXB/XJC utility and that generates your Java classes. The resulting classes are just beans (empty constructor and getters and setters for each field). But, depending on what plugins you specified in the XJC command, there might be other stuff as well.
Now, for Madura Objects, what I did was add interceptors into the resulting setters. These do some logic based on the annotations on the relevant field and this allows them to do field validation. The annotations can be specified in various ways in the XSD file, so you get declarative validation completely transparent to your surrounding code.
This is different from Hibernate Validation (though I have kept the annotations as similar as possible). Hibernate Validation assumes you call the setters and then call a validator method. MaduraObjects does it automatically and it rejects the value without actually setting it. That means your objects are always clean and you don’t have to write fixup logic to undo bad values.
There is somewhat more but I’ll go into that next time. But you can check it out right now at Madura Objects.