Blog

  • Passing ActionScript Classes to ColdFusion via Remoting

    Something I just found out today is you can pass ActionScript classes to a CFC method via Flash Remoting. I was under the impression that this only worked in OpenAMF, AMFPHP, and Flash Remoting for Java. I have publicly stated in the past that it cannot be done, and I was wrong in doing so.

    Typically, the structs I get back from ColdFusion are either objects with properties that have uppercase names, or arrays of structs. I write Factory code in either Flash or Flex, ARP or Cairngorm, to convert these transfer objects to value objects which are basically ActionScript classes that match their CFC counterparts, property for property. This is done so both I and the server-side developer can be on the same page when we discuss what a “Person” really means. If we both have a class that has data for the person defined in properties in our respective languages, it allows us to communicate effectively, both in code and over the phone, email, and IM.

    A lot of time is spent writing Factory code, and is one of my frustrations with using Remoting with ColdFusion. OpenAMF isn’t any better if you don’t have a Java guy with Flash Remoting and/or OpenAMF experience. While your VO’s may technically work, you’ll spend weeks configuring it. AMFPHP just works.

    However, as of today, I found out you CAN send ActionScript classes instead of just plain Object’s to a CFC method as long as you do 2 things:

    1. Wrap it in an Object with 1 property. That property must be named the same as the cfargument tag
    2. Make a deep clone of the VO you are sending back

    If your CFC’s cfargument tag looks like this:

    <cfargument name="someVO" type="struct" required="false" default="" />

    Then you’re service call in ActionScript looks something like this:

    var clonedVO:SomeClass = myVO.clone();
    service.method ( {someVO: clonedVO} );
    

    Notice how the someVO matches the cfargument tag’s name. The first part makes sense, but the clone still baffles me. Regardless, it works, and cuts down on 40% of the time I spend writing Factory code to transpose ActionScript value object classes to plain old vanilla objects.

    This post is irrelevant with Flex 2 and ColdFusion > 7.0.0 since Remoting works like you’d expect it to (at least, from what I’ve seen in the demo’s, haven’t played with Mystic yet).

  • Chunk vs. Picard, Juggernaut, & Ruby on Rizz-nails Weekend

    *** Warning: links are not work safe ***

    BANG, BANG sucka (turn up your speakers), I’m the Juggernaut. Imma hit chu wif ya own pimp!

    Talked to Steven Wednesday night, guy who sent me bang bang, about Ruby. I got to ask a bunch of questions about it and apparently, it does all the crap I hate doing in back-end code for me. Her majesty’s gone for the week, so I’m going to give Ruby & Rails a run through this weekend; gotta bunch of docs to read + 2 hosts. Ben Jackson I believe already wrote a Remoting-esque implementation for Flash so at least this isn’t 100% new territory.

  • How to trace every remoting call without ServiceCapture

    This isn’t a replacement for ServiceCapture; it should definately be used when you are doing some serious debugging between your client and server (or HTTPLook which isn’t as good with AMF traffic). However, if you have your own debug window, or just want another way to do it all in Flex 1.5, Flash MX 2004, and Flash 8, here’s how:

    import mx.services.Log;
    import mx.remoting.Service;
    
    private static var contructedApp:Boolean = contructApp();
    
    static function contructApp()
    {
            Log.prototype.onLog = function(message:String):Void
            {
                    trace(message);
            };
            _global.mainLog = new Log(Log.DEBUG, "mainLog");
            Service.prototype.log = _global.mainLog;
    }
    

    I believe that’ll show HTTPService calls for Flex 1.5 as well. Naturally, you can route trace in the onLog to whatever you want, or even use a Delegate to have the onLog function point and/or do something entirely different.

  • ValueObject Grind

    The backend developer is done, and loads of CFC’s are freshly down from Subversion for my perusal. Since I’m using Cairngorm for this Flex 1.5 project, I do a series of enapsulated steps to tie my front-end to the back.

    1. Open his CFC’s, find a method I need, and write a business delegate for it
    2. write a Command to launch the Delegate, and post it’s data on the ModelLocator
    3. add the necessary command constant names for EventBroadcaster in the Constants & Event classes
    4. open up the CFC value objects that the back-end developer has done, copy and paste the variable names into a Value Object class, and finish the class
    5. Since we are using ColdFusion pre-7.0.x, we can only get generic transfer objects back from the server (unlike AMFPHP or OpenAMF). So, we have a Factory class create Value Objects from the Transfer Objects ColdFusion sends back in the Delegate. I write the methods to create them as well as give back arrays of them.

    …this is where things start to suck. It’s not hard; you just convert uppercase property names to camel-case’d Value Object class property names like so:

    public static function getUserVOFromUserTO( p_to : Object ) : UserVO
    {
            var user : UserVO = new UserVO();
            
            user.firstName = p_to.FIRSTNAME;
            user.lastName = p_to.LASTNAME;
            user.userID = parseInt ( p_to.USERID );
            
            return user;
    }
    

    However, our value objects have a lot more property names, some have arrays of other vo’s they link to, and we have a lot more value objects since this is an extremely large project.

    Hard? No. Tedious? Damn straight… It is extremely important we get strong-typing to ensure the data we are using is correct and easier to debug if we find a problem. Using a for loop in the above with a toLowerCase() could work, but then you are assuming that data sent from the back-end is correct. Even worse, while CFC VO’s are somewhat easy to read since most properties are up top, some have linkages with other items (because of linked tables, many to one relationships, etc.), thus you cannot necessarely view it as a 1 to 1, CFC to client side VO relationship.

    A monkey could do this shit. That’s the rub; most programmers who make money start to recognize long, repetitive tasks, and the first question they ask themselves is, “Can this be automated?”.

    Flex 2? Sure, right click on a CFC, generate ActionScript VO. Flex 1.5 using ColdFusion >7.0.x? Nope, not without sacrificing strong-typing. You can get away with that in smaller scale apps, but not enterprise applications.

    BLEH!

    …speaking of monkey’s, maybe I could teach a Bathroom Monkey to code VO / TO conversion functions… hrm…

    My manager says writing CRUD operations is worse (which is automated in Flex 2 as well btw for ColdFusion). I certainly don’t evny the back-end guys, and will happily make VO’s in this Factory all day and smile when I think of what they most go through.

    BTW, I’ve unsubscribed from the 10 billion email lists I was on, so if you don’t see me answering questions, it’s because I am not receiving any emails (Flashcoders, Flexcoders, etc.). I need a break from lists.