October 2006 Archives

Convert Generic Objects into Class Instances

| 3 Comments | No TrackBacks

This question has come up a lot on FlexCoders recently, so I wanted to offer to the community a generic solution to convert plain old vanilla objects into full-fledged class instances.

I had originally developed this solution to be used with the JSON library that I made for the Adobe Labs Corelib project, but it's really an all-purpose solution. Essentially, my utility method allows you to turn any remote service that returns plain old ActionScript objects (like JSON, WebServices, etc) into a service that returns typed class instances, much like RemoteObject.

This makes it easier to employ the ValueObject pattern... instead of getting back an object with some properties attached, I can convert that object into, say, a Book ValueObject. The Book then is easier to work with because I get IDE and compiler support from strong typing, and it also speeds up the code execution since accessing values in typed objects executes faster than in non-typed objects.

You can download my ObjectTranslator, and its test cases, here. (MIT License)

Usage is as follows:

import com.darronschall.examples.vo.Book;
import com.darronschall.serialization.ObjectTranslator;

// Define an object with properties that mimic the variable names
// inside of the Book class
var bookObj:Object = { title: "My Book title", pageCount: 10, inLibrary: true };

// Convert the generic object into an instance of the Book class
var book:Book = ObjectTranslator.objectToInstance( bookObj, Book ) as Book;

Yup, it's really that simple.

Unfortunately, things get a little more complicated when you have nested ValueObjects. I didn't take the time to make my method recursive, so it's up to you to first convert the nested objects into instances, and then convert the top level object into an instance. It's a little more work that can avoided by some recursion. I've outlined what the algorithm would look like inside of the code, I just haven't had the time to write it yet.

import com.darronschall.examples.vo.Book;
import com.darronschall.examples.vo.Student;
import com.darronschall.serialization.ObjectTranslator;

var studentObj:Object = { firstName: "test first",
					  lastName: "test last",
					  favoriteBook: { title: "Favorite Book!" }
					};	
								
// First we need to convert the nested objects to classes
studentObj.favoriteBook = ObjectTranslator.objectToInstance( studentObj.favoriteBook, Book );
		
// Convert the student object to a Student class
var student:Student = ObjectTranslator.objectToInstance( studentObj, Student ) as Student;

So, to summarize, the objectToInstance method allows JSON and WebService services to return true class instances from the server, making them behave like RemoteObject and allowing developers to more easily employ the ValueObject pattern. Use the objectToInstance method on the object that the service returns, and then use the converted instance elsewhere in your code.

Those who are curious about the implementation will get a kick out of the simplicity of my algorithm. Essentially, all I'm doing is constructing an AMF packet with the class name and the object property values, then doing a byteArray.readObject() to convert the AMF packet into a class instance. The code is fairly well commented so you can follow along easily. I'd like to think this is one of those "so simple it's brilliant" type of moments...

Have fun, and let me know if you run into any issues!

Tags: , ,

The Cookbook is here...

| 4 Comments | No TrackBacks

I received my author copy of the ActionScript 3.0 Cookbook in the mail yesterday.

ActionScript 3.0 Cookbook

Amazon.com is reporting the book as in stock, but some people have said that they received notice that it won't be shipping until December. I'm not sure why there's such a big discrepancy there - I know the book is going to be available at Adobe MAX, which is a little over a week away, so I'm a little confused myself.

Regardless, I'm glad it's finally printed and out the door. I believe the ActionScript 3.0 Cookbook is the first published book on ActionScript 3.0, and the first book avaialble as part of the Adobe Developer Library. I had a great time writing it with Joey and Keith, and I hope you enjoy reading it and learn a thing or two from us. :-)

Multiple Inheritance in ActionScript 3

| 11 Comments | No TrackBacks

Did you know ActionScript 3 supports multiple inheritance? Here's how...

Multiple inheritance is actually possible with AS3, despite popular belief that it's not supported. It's not really multiple inheritance in the true sense of the word going by the strict definition... but there's a way to get the job done in AS3 that behaves almost like the real thing.

What I'm about to show you is not something I would advocate as best practice. Nor is it something that should be (ab)used simply because its available. I'm not going to get into the debate about Composition vs. Inheritance and how Multiple Inheritance fits into the picture. If you're here, it's probably because you want to know how to use this technique. I assume you also know then the various repercussions (and if not, at least have the ability to google them). As they say, "with great power comes great responsibility".

Various disclaimers aside, here we go...



About this Archive

This page is an archive of entries from October 2006 listed from newest to oldest.

September 2006 is the previous archive.

November 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Archives

OpenID accepted here Learn more about OpenID
Powered by Movable Type 5.02