After some very encouraging remarks from Ark on this post I decided to make my Titles as boring as possible so all the left brain people in the world ( read about them here ) can understand me. If you want to tune out now now I’ll understand.
Or not, I’m really just interested in blogging about technology so I’ll keep to my thought and talk about using a Base Value Object . One of the things I never do is set individual properties on objects. I should be able to pass them an object, and they should know how to set themselves. I demonstrated this with that horribly named post here, and I wanted to follow up that post with the technique I use to create value objects.
The concept is as follows: You have a lot of different value objects and no matter what you throw at them they know how to populate themselves, so that whether it’s an object returned from the server or it’s a selectedItem from a DataGrid the call to create a new value object should be the same. for example:
var
user : UserVO = new UserVO(selectedItem);var user : UserVO = new UserVO(event.result);
My UserVO looks like this. Notice it passes the args to the super function, and it’s only responsibility is to define what it looks like.
public
class UserVO extends ValueObject{
public function UserVO(args:Object=null)
{
super(args);
}
public var name:String; public var email:String;}
And finally my Value Object that does all the work.
public
class ValueObject{
public function ValueObject(args:Object=null):void
{
setVO(args);
}
public function setVO(args:Object=null):void
{
if(args == null)
return void;
for (var key:String in args)
{
// for some reason mx_internal_uid gets in the way. if (key != "mx_internal_uid" && this.hasOwnProperty(key))
this[key] = args[key];
}
}
}
My base value setVO function is where everything takes place. You can make your objects dynamic and take out the check for hasOwnProperty(). Also, Examine the type of the args passed in to the setVO function to make it possible to pass in xml, or arrays, or any other sort of data type, I took out my checks for the purpose of brevity.
Well hopefully this post was drab enough to win over Ark. If you’re still in the mood, check out this.
brian..
Not bad my friend, not bad. I enjoyed the riffs on Ark the Snark as well. :)
Posted by: Ben | February 05, 2008 at 01:12 PM
Nice idea! Thanks for sharing.
Posted by: Cesare | February 09, 2008 at 05:38 AM