Search |
||
Converting objects from A to B and back - there ought to be a libraryPosted by timboudreau on May 1, 2009 at 11:33 AM PDT
One pattern that is an incredibly frequent recurring theme is converting an Object of type A into an object of type B for something that understands B to consume. Tons of libraries have something like this embedded in them - beans binding, pretty much anything that validates strings. If there were ever something that deserves a simple common library, it's this.
I was just looking at Fabrizio Guiduci's Better Beans Binding project, which took me to the original beans binding project. Embedded in it is a mini-framework for converting objects back and forth between types - an application provides a List, a JList wants a ListModel.
Last week as part of my new Simple Validation project I wrote into it a mini framework for object conversion and a converter registry:
public abstract class Converter<From,To> {
public abstract <To> convert (From from);
public static <From,To> Converter<From,To> find (Class<From> from, Class<To> to);
}
Java Beans property editors are de-facto Object -> String -> Object convertors - I've long thought that one of the bigger messes in the beans spec (there are so many!) is the fact that type conversion is a completely orthagonal concern that should have been factored out.
And on it goes - I'm sure there are many, many more examples. All of the subclasses of java.text.Format in the JDK count. Pretty much anything that communicates via HTTP and uses an object-oriented language is one.
Perhaps something like this already exists and I don't know about it.
But what I would really like to see is a simple framework that solves the problem, that the rest of these projects could use - it's really an area where there is endless spinning of wheels. The characteristics something like that would be:
»
Related Topics >>
Java Patterns Comments
Comments are listed in date ascending order (oldest first)
Submitted by ljnelson on Fri, 2009-05-01 11:45.
I always wanted this to be implemented properly: http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Beans.html#getInstanceOf(java.lang.Object,%20java.lang.Class)
Best,
Laird
Submitted by amusi on Sat, 2009-05-02 02:57.
Maybe Dozer would help: http://dozer.sourceforge.net/
It's a framework for copying information from a to b, configurable with xml files, using convention over configuration. It really can do a lot of things, I've been using it for very simple things though.
regards,
amusi
Submitted by rondeth on Mon, 2009-05-04 07:35.
FWIW, we tried Dozer and found some features lacking--and the source was a relative mess at the time. We had much better luck with Morph:
http://morph.sourceforge.net/
|
||
|
|