[flexcoders] Re: Casting down an object

2009-11-02 Thread droponrcll


--- In flexcoders@yahoogroups.com, "bnsmeets"  wrote:
>
> Hi all,
> 
> I'm wondering if it is possible to 'hack' some sort of downcasting into AS3. 
> The context is, that I am using a generated webservice proxy from Flex 3, and 
> want to be able to add custom (extra) properties to the generated 
> valueobjects without having to adjust the code that parses the wsdl result 
> into objects.
> 
> So e.g. the webservice generates the class:
> 
> class A
> {
>   property A;
>   property B;
> }
> 
> I have my own class that is:
> 
> class B extends A
> {
>   property C;
> }
> 
> When i call the generated webservice "get all A's", it will return an array 
> of "A" objects, what I am looking for is to convert that list into "B" 
> objects. So the values of the properties that exist in the "A" counterpart, 
> filled for a new "B" instance, with an empty "C" property.

Instead of having B extend A, instead have both A and B implement Interface I.  
Then use the Decorator pattern (there's a current feature on this on InsideRIA 
http://www.insideria.com/2009/10/decorator-design-pattern.html if you don't 
have the book AS3 Design Patterns) to have B "wrap" an A and "reflect" the 
properties that I requires it to have.

So 

package {
  public interface I {
function get prop1():String;
function set prop1(s:String):void;

function get prop2():String;
function set prop2(s:String):void;
  }
}

package {
  public class A implements I {
private var _prop1:String;
private var _prop2:String;
public function get prop1():String {
   return _prop1;
}
public function set prop1(s:String):void {
   _prop1=s;
}
public function get prop2():String {
   return _prop2;
}
public function set prop2(s:String):void {
   _prop2=s;
}
  }
}

package {
  public class B implements I {
private var _a:A;
private var _c:String;
public function B(a:A) {
_a=a;
}
public function get prop1():String {
   return _a.prop1;
}
public function set prop1(s:String):void {
   a.prop1=s;
}
public function get prop2():String {
   return a.prop2;
}
public function set prop2(s:String):void {
   a.prop2=s;
}
public function get c():String {
   return _c;
}
public function set c(s:String):void {
   _c=s;
}
  }
}

Now, any code that needs type information, you'll be looking for I, not A, 
because a B is not an A, it "contains" an A.  For more on Interfaces, check out 
http://www.insideria.com/2009/10/interfaces-and-dynamic-class-i.html or 
http://flexdiary.blogspot.com/2009/01/example-of-casting-contets-of-swfloader.html
 .


HTH;

Amy



[flexcoders] Re: Casting down an object

2009-11-02 Thread jer_ela
Do you need A class objects (ie ones w/o the C property) in your app?
If not just add the C property to the A class.  It should still be converted 
correctly. 

If you still need the A class, add an exportAsB method to create a B class 
equivelent.

--- In flexcoders@yahoogroups.com, "bnsmeets"  wrote:
>
> Hi all,
> 
> I'm wondering if it is possible to 'hack' some sort of downcasting into AS3. 
> The context is, that I am using a generated webservice proxy from Flex 3, and 
> want to be able to add custom (extra) properties to the generated 
> valueobjects without having to adjust the code that parses the wsdl result 
> into objects.
> 
> So e.g. the webservice generates the class:
> 
> class A
> {
>   property A;
>   property B;
> }
> 
> I have my own class that is:
> 
> class B extends A
> {
>   property C;
> }
> 
> When i call the generated webservice "get all A's", it will return an array 
> of "A" objects, what I am looking for is to convert that list into "B" 
> objects. So the values of the properties that exist in the "A" counterpart, 
> filled for a new "B" instance, with an empty "C" property.
> 
> Any ideas? 
> 
> Cheers,
> 
> Ben
>