Working with raw data and objects in FlashBuilder

October 12, 2009

FlashBuilder 4 Beta 2 was released as part of MAX 2009 and with it a new set of features for data centric development (DCD). Sujit has a series of posts on DCD at http://sujitreddyg.wordpress.com.

One of the goals of DCD is to enable the developer to work with data in an object oriented way irrespective of the data format (XML, JSON, AMF) or the data exchange protocol (HTTPService, SOAP) with the back end.

The classes in Flex 4 sdk through which data could be got are HTTPService, HTTPMultiService, WebService and RemoteObject

HTTPMultiService

HTTPMultiService is a new class that is added to the flex sdk and supports serialization of data through a property called serializationFilter – you can find more info here . This enables one to convert the HTTP response which could be XML or JSON to a ActionScript class. This is accomplished by the default XML and JSON serializers that are there as part of serializers.swc that comes as part of the FlashBuilder project when you use the Data Menu to define a service this swc gets added as part of your build path.

The serializers are

  • com.adobe.serializers.xml.XMLSerializationFilter
  • com.adobe.serializers.json.JSONSerializationFilter

Also, one could plugin their own serializers or override the functions of the default serializers to add custom behaviors while decoding or encoding XML / JSON data in a flex project.

Note: One could still continue to use E4X as the means to access XML responses, but working with strong types and AS classes gives much more flexibility and results in more maintainable code.

WebService

WebService class has not changed much since Flex 3, where one can use the the schemaregistry to convert the result SOAP response to AS classes. Check the section “Customizing web service type mapping” in the link http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_3.html. Though sometimes, the response itself will not be indicative enough of what is being returned (check this http://www.webservicex.net/uszip.asmx?wsdl) which tells the result is of type “any” and we cannot use the schemaregistry to register a class also for this so that the webservice class converts this to a appropriate AS class (check the bug https://bugs.adobe.com/jira/browse/SDK-22574 )

This has subsequently been resolved by providing a convertResultHandler property in WebService which could be set to a function which can there by convert the returned result to any AS class that the developer wants.  The signature of convertResultHandler is

public static function convertResultHandler(result:*, operation:AbstractOperation):*
{

}

Notice that this could be used across operations and based on the operation one can convert the result which is sent as the parameter and to an AS class that could be referenced in the resultHandlers of those operations.

RemoteObject

Most PHP, CF developers love their platform because of the flexibility it provides with respect to typing of objects and they have a choice to use strong types i.e. PHP classes or CFC components or just work without classes. FlashBuilder provides a unique way to work with weak types on the server and can convert them to strong types on the client without the developer having to do anything on the client side (ofcourse you might have to invoke a wizard and type in a name for the strong type, which is not hard :-), compared to writing your own client side data layer and doing all the conversions of data yourself )

Check the “Configuring return type” section in http://sujitreddyg.wordpress.com/2009/10/05/building-flex-applications-for-java-based-http-services-using-flash-builder-4/

This will set you up with a return type for your data returned from PHP or CF.

But you feel that you might need additional massaging of the data returned by the server or change the type of a particular property. You can customize again by using the convertResultHandler which is again used for converting a RemoteObject response to a strong type.

you can find this in the _super_* class file’s constructor generated by FlashBuilder in the following piece of code

_serviceControl.convertResultHandler = TypeUtility.convertResultHandler;

TypeUtility is a utility class defined in serializers.swc.

The message here is that

  • FlashBuilder has a robust framework to work with weak data types in a object oriented way
  • There is a good customization mechanism to override and extend the default conversion mechanisms
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: