Multiple Representations in Object/XML Mapping
Having tried a few different approaches for Object/XML mapping over the years, from SAX/DOM manually coded work, through Rails' (ActiveRecord) toXml, JAXB, XStream, Hibernate, etc, I'm constantly surprised by the idea that most of these frameworks seem to assume that most people are going to want a single representation of an Object in XML form.
In almost every application I've ever built that did any kind of Object/XML mapping, I've wanted to have multiple representations of the objects. In particular, I'm most likely to want these three:
- A simple reference - in this form, I'd like to link to the object, and provide almost no additional information. This is usually used to link from one object to another.
- A summary - in this form, I'd like to provide just enough information to populate a list, or a combo-box, but no more. I'd usually use this to populate a list or a combo box.
- The full details - in this form, I'd like to provide most or all of the information about the object. This is for detailed views of the object.
- Reference<contact id="123" href="http://my.rest.context/api/version/contacts/123.xml" />
- Summary<contact>
<id>123</id>
<name>Joe Blow</name>
<email>joe.blow@example.com</email>
</contact> - Full<contact>
<id>123</id>
<name>Joe Blow</name>
<email>joe.blow@example.com</email>
<firstName>Joe</firstName>
<lastName>Blow</lastName>
<address>1 Yonge Street, Toronto, ON</address>
</contact>
I've not yet run into an Object/XML mapping framework that made this kind of control easy (do feel free to recommend some, if you like), so I've often felt like a templating approach, the same kind of approaches one uses to generate HTML, makes as much sense as any. In Rails, this takes the form of RXML templates. In Java, this could be Velocity/Freemarker, or something of that ilk.
Suggestions for another approach are welcome.
No comments:
Post a Comment