I have a design pattern for CFCs that I like to use, but recently I ran into an annoying problem where I was trying to use some of my already in-use CFC with a client written in flex, consuming the CFCs as web services.
By finding a nice tip on Kyle Hayes' blog (see here), I was able to get things to working just the way I had hoped, and this blog entry will show how it all comes together.
Here is the basic structure of my standard CFC template:
<cfcomponent>
<cfset my = StructNew()>
<cfset my.getList = structNew()/>
<cfset my.getList.error = 0/><cftry>
<cfquery name="qrsRESULTS" dataSource="MY_DATASOURCE">
SELECT col1,col2,col3
FROM SOMETABLE
WHERE somevalue = '#ARGUMENTS.somevalue#'
</cfquery><cfset my.getList.result = qrsRESULTS>
<cfcatch type="any">
<cfset my.getList.error = 1>
<cfset my.getList.message = CFCATCH.message>
</cfcatch></cftry>
<cfreturn my.getList>
<cffunction name="getStuffAsQuery" returntype="struct" access="remote">
</cffunction>
<cfargument name="somevalue" type="string" required="true">
</cfcomponent>
Note that the return value is "Struct" even though I am wanting to return a Query object (which Flex will auto-interpret to be an ArrayCollection). Also note that I am constructing a structure with the named "my.{function name}" where all the values are stored.
The main reason for making your CFC this way is so that you have a predictable way to check if each and every method call succeeds and a predictable way to get the results. You can check the .error value of every call for success, and in your application (whether it be CF or Flex), take the appropriate error-handling steps.
In Flex, you would define your webservice for the above CFC like so:
<mx:WebService id="wsdlService1" wsdl="http://www.host.comspam/stuff.cfc?wsdl" showBusyCursor="true">
<mx:operation name="getStuffAsQuery" result="handleMyResult(event)" fault="errorHandler(event)">
<mx:request xmlns="">
<somevalue>{this you have to provide}</somevalue>
</mx:request>
</mx:operation>
</mx:WebService>
To execute this web service call, you would do the following:
wsdlWhitelist.getListAsQuery.send()
When this call completes successfully, it will call the "handleMyResult()" function, and if it fails, it will call the "errorHandler()" function (see above code for these references).
Lets look at the "handleMyResult()" function:
public function handleMyResult(event:ResultEvent):void
{
if (event.result.ERROR == 0)
{
Alert.show("Data received properly!");
// query results are in variable "event.result.RESULT"
// Flex will automatically interpret this as an ArrayCollection
// so you can assign it as the data provider for a DataGrid, etc.
}
else
{
Alert.show("There was an error loading data: " + event.result.MESSAGE);
}
}
In this function, the result event is passed in. "result.event" contains whatever came back from the web service, which in our case is a ColdFusion struct. Flex will interpret this as an untyped Object, just like a Flash Object. The struct members from ColdFusion are there, but are object properties now. The important thing to note here is that the names of the object properties that came from our ColdFusion struct are now all caps. In the CFC code, we had a property called "error" -- but in Flex, we have to reference that as "ERROR." Likewise, the RESULT and the MESSAGE properties are from our ColdFusion struct.
More later, and if you have questions, feel free to ask.