Using __type__ to return objects to Flex from ColdFusion faster

Submitted by Falken on

I've been playing with the method that seems to have been hiding from everyone for a while at http://www.briankotek.com/blog/index.cfm/2008/1/28/Returning-Typed-Structs-vs-CFCs-to-Flex
to generate arrays of value objects based on a query, but without using createObject().

You might want to search for '__type__' in
http://groups.google.com/group/transfer-dev/browse_thread/thread/0abdb2191dcfd226#2cbf827754e75376 and
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=UseFlexDataService_05.html

The whole* community* seems to have discovered __type__ by accident (though it's in plain sight in the docs !), and missed it during the big debates last year about returning VOs being slow. The basic idea is that the magic that sits between ColdFusion and a Flex client can use a struct key called '__type__' to tell Flex that this item isn't a struct, it's an object.
If a CFML page is consuming the result, then it doesn't care as much as Flex does about the types, and resultArray[i].AValueObjectPropertyName' is still a valid variable, so no code changes needed there either.

Below is a code snippet.
This is much faster than using CreateObject() for each array item, AOP (in ColdSpring) can of course be used to perform this conversion for you, every time you return a query.

<cfset res=ArrayNew(1)> 
<cfset props=arguments.result.columnlist>
<cfoutput query="arguments.result">
<cfset res[arguments.result.currentRow]=structNew()>
<cfset res[arguments.result.currentRow]['__type__']=variables.queryObject>
<cfloop list="#props#" index="prop">
<cfset res[arguments.result.currentRow][prop] = arguments.result[prop]
[arguments.result.currentRow] >
</cfloop>
</cfoutput>
Sections