Richard Rodseth wrote:
> Has anyone done the work to enhance Darron Schall's ObjectTranslator
> class to work recursively, as he describes in the source code?
> 
> In the absence of that, I was think of adding static factory methods
> to my VO objects, as follows:
> 
> static public function newInstanceFromJSON(json : String) : StudentVO {
>       var student:Object = JSON.decode( json );                       var
> studentVO:StudentVO =
> ObjectTranslator.objectToInstanc(student,StudentVO);
> 
>       for ( var i:int = 0; i < studentVO.books.length; i++ )
>       {
>               var bookVO:BookVO = ObjectTranslator.objectToInstance(
> studentVO.books[i], BookVO ) as BookVO;
>               // Use the converted class instance in place of the regular 
> object
>               studentVO.books[i] = bookVO;
>       }
>       return studentVO;                                               
> }
> 
> Comments welcome.
> 

I use a return type of array to obtain a dynamic object(DO) object graph 
from a HTTPService call that returns xml. I pass the result object to 
the top most VO, which then inits the graph of VO objects(using factory 
methods).
The DO is adapted/proxied/decorated by the VO so there is no copying of 
data from dynamic to strongly typed; the functions calls are proxied to 
the DO.

This provides a lot of control and has a few benefits for me:

. Able to access and use the internal dynamic object if required without 
making the VO class dynamic.

. Generated "_Classes" make changes to xml returned from server very 
easy to manage. New properties or event handlers etc. can easily be 
added to all model objects.

. Able to use the internal dynamic object to JSON encode the 
return(POST) data.

. Use of strongly typed objects within AS and mxml.

. Conversion from booleans to "Y", "N", "A" "I" (yes/no/active/inactive)
values or Dates to String types is straight forward.

Eg)

class XYZ  extends _XYZ{

   //xyz has many abc objects.
   private abcObjects:ArrayCollection;
        
   //called on result of a http service invocation
   public static function newInstance(rootObj:Object):XYZ{
       //create and return an instance of XYZ.
       //init the children objects by calling there factory function
       var obj:XYZ = new _XYZ(rootObj);
       obj.initABCObjects();                            
       return obj;
   }

   protected function initABCObjects():void{
     abcObjects = new ArrayCollection();
     if (super.oData.abcObjects){
        //loop creating and adding to abcObjects using
        //ABC.newInstance(abcObjects[i]);
     }
   }    
}


//perl generated AS class contains functions to access simple properties
// complex properties of VO's are handled in the subclass.
class _XYZ {
   private var _o:Object;

   public function _XYZ(o:Object){
     _o = o;
   }

   public function get oData():Object{
     return _o;
   }

   public function getName():String{
     return _o.name;
   }

   public function setName(name:String):void{
     _o.name = name;
   }

}

There are probably better ways to do this but I've had reasonable 
success to-date with the approach described above. The generated code 
really makes it easy when things are constantly changing during development.

regards,
   - shaun

Reply via email to