Hi Ian,
Ian Shafer wrote:
> Hello,
>
> I'm getting XML from an HTTPService call (and/or a WebService call, I
> don't think this matters). I want to take this XML and put it into my
> AS3 objects (ValueObjects using Cairngorm terminology).
>
> I'm currently using Darron Schall's ObjectTranslator, which is great but
> doesn't work for rich object graphs.
I had the same problem. I decided to try a different approach, which is
working quite nicely for me so far.
Basically I use a pattern which could be described as either Proxy,
Adaptor or Decorator, any or all of them I think..
The basic idea is to use composition. It looks something like this:
//Adaptor, Decorator, Proxy class.
public class Product extends _Product{
//An array of Strongly typed ProductDetail objects.
private var detailObjects:ArrayCollection;
// rather than unpacking all the objects I just proxy the
// method invocations to the dynamic object,
// that way I can work with STOs and have less work to do with
// maintaining variables for packing/unpacking etc.
// I also generate the super classes to make things quicker.
public function Product(obj:Object){
super(o);
// The Dynamic object is only the values for this object, not its
// detail objects. This allows greater flexibility at JSON encoding
// time.
this.detailObjects = convertToStronglyTyped(this.o.details);
this.o.details = null;
}
public function getAllDetails():ArrayCollection{
return this.detailObjects;
}
public funtion available(): Boolean{
return super.isAvailable == "Y";
}
public function setAvailable(b:Boolean){
super.isAvailable = (b) ? "Y" : "N";
}
public function addedDetails():ArrayCollection {
//return only the detail ids that have been added
}
//Used to send a customised version of the objects to the server.
public funciton addedDetailsJSON():String{
//Only send the id's of the details that have been added.
o.addedDetails = addedDetails();
return JSON.encode(o);
}
}
//This is a generated class.
public class _Product {
protected var o:Object; //returned by my httpservice.
public function _Product(obj:Object){
this.o = obj;
}
public function get name():String{
return this.o.name;
}
public function get isAvailable():String{
return this.o.isAvailable;
}
etc...
}
Not sure how well this will go in the long run. Time will tell I guess.
I have a pretty simple model to work with, it might become a real pain
with a larger object graph. Dunno.
A couple of other things about this pattern that seem to be of benefit.
1) I can still acess the internal object dynamically, without making my
VO dynamic. This has proved to be a benefit so far in one or two
circumstances where it has been acceptable for me to use the dynamic
objects instead of the VO. ie) object[property] access.
2) I imagine, performance might be improved when converting large sets
of objects into VOs. I havent done any actual testing though, my data
set wont get much larger than a 100-200 items, so havent bothered.
cheerio,
shaun