Marc Hughes


Home
Blog
Twitter
LinkedIn
GitHub
about
I am a developer from a bit west of Boston.

XML Facade instead of value objects?

19 Nov 2007

I have a project I'm working on where it'd be great if older versions of the software preserved information in the XML file format that it didn't understand. For example, imagine if Version 1 (V1) of the software had this for a file format:

<data>
<value>1</value>
</data>

Now imagine if V2 of the file added an attribute

<data>
<value type="number">1</value>
</data>

It'd be great if you opened that second file with the V1 software and then saved it again, it would preserve the stuff it didn't understand. Unfortunately that's not how I usually write my value objects. Usually I do something like:


public class ValueObject
{
public var value:Number;
public static function fromXML( xml:XML ) : ValueObject
{
var v:ValueObject = new ValueObject();
v.value = xml.value;
}

public function toXML( ) : XML
{
var xml:XML = <data>;
xml.value = value;
return xml;
}
}


As you can see, anything in the file that it doesn't understand is lost. But what if we followed a facade pattern for our data objects and did something more like this:


public class ValueObject
{
protected var source:XML;

public static function fromXML( xml:XML ) : ValueObject
{
v.source = xml;
}

public function toXML() : XML
{
return source;
}

public function get value() : Number {return source.value;}
public function set value(val:Number) : void {source.value = val;}
}


They both have the exact same API, but the second one will preserve XML attributes (or even nodes) that it doesn't understand.

What about AMF based projects, especially when passing rich objects with a Red5 server? I know there's a pretty seamless mechanism in place if properties aren't known, but how do you get those unknown properties back to the server?

What other solutions or best-practices do other people follow for solving this issue?