Welcome to my blog! Feel free to post comments.
- Andrew


Visit Coldfusion Community


<< October, 2007 >>
SMTWTFS
123456
78910111213
14151617181920
21222324252627
28293031
Search Blog

Categories
Archives
RSS


Powered by
BlogCFM v1.14

Designing CFCs for use with Flex
28 October 2007

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>

<cffunction name="getStuffAsQuery" returntype="struct" access="remote">
     <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.

Posted by aschwabe at 12:00 AM | Link | 0 comments


pfSense - Router OS
02 October 2007
I recently started evaluating load-balancing solutions for our small office.  Verizon seems to never have a clue, and loves to spontaneously shut accounts down for no good reason, and without warning.  For any of your Verizon DSL or FiOS users -- beware!

I have begun evaluating a hardware device called the HotBrick LB-2 which supposedly is designed specifically for taking two WANs and load balancing traffic (with failover).  Exactly what I want. While looking at prices, I came across discussion of an open source project called pfSense.

Now normally I will consider open source for applications I can customize, but I never really considered running something like this in place of our router or firewall.

Documentation is scarce, but I have been discovering some impressive things about pfSense:
  • It is based on m0n0wall -- an excellent BSD router OS with a web management interface
  • It can be run off a Live CD !!!
  • You can set it up to run in production off the live CD and save your changes to a USB key (no hard drive!!!)
  • It has built in support for load balancing WANs
  • It has built in QoS (Quality of Service) weighting, and it works for VOIP (Voice over IP)
All of these things add up to something that is a lot less scary... sort of...

I am impressed enough that I will being a new project on the side (yeah, that means it will take a while) to setup a pfSense box for our office.  I expect my specific challenges will be around configuring the DMZ for our servers, which use static IPs on ONE of our WANs.  Once its load balanced, hopefully incoming traffic will go to the right place for the DMS, and outgoing (and VOIP phones) will be load balanced both ways.
Posted by aschwabe at 6:50 PM | Link | 0 comments