Real World AJAX Presentation Code

Last night I presented my "Real World AJAX" talk to PDXRIA (our local Portland Adobe User Group). I said I would post the code. So, here it is! (Download link below)

I have included the PDF slide presentation too. There are not a lot of slides. This is a code heavy talk. It uses a very simple CRUD application to demonstrate adding AJAX to an existing application. There are three versions in the zip. One completely paged based. Another one with some 'partial page update' style AJAX to improve the user experience. And a final one with some DHTML and DOM building. The code uses an in memory query object instead of a database. So, you should be able to drop it in a folder and run it with no setup.

When I give the presentation, I show how you can turn JavaScript off and the second version still works (it gracefully degrades). I really need to add about 10 more slides with some of the stuff that I ramble off while I'm showing the code examples. If I get around to polishing it off, I'll re-post the updated slides and code.

Also, I built the slides in OpenOffice.org Impress. If you would like to give this talk to your user group, or somewhere else, let me know.

Database Abstraction With Illudium and onMissingMethod()

This is the long overdue wrap-up post in my series about using Illudium outside it's Flex front end as a service to generate CFCs.

To recap: I explained why I was doing this and with these tools. Then I showed how you could call Illudium outside of it's Flex front-end. And I showed how we could bundle that generation code up into a CFC.

So, here's where it really gets fun. Now, I can generate CFCs to whatever specification that I need. But, I definitely do not want to be writing a bunch of CreateObject()s everywhere with hard coded paths to beans and Gateways. So, I decided that a dbService (service object to hide the implementation details of my database CFCs) would be a very good idea. I also decided that, like Reactor, I would do run-time generation to speed up developing code for new tables.

I also decided that I would create an API where methods have the name of the table in them (like getUserGateway(), getNewUser(), and getUserByID(userid). This is made possible by the onMissionMethod() method of CFCs in ColdFusion 8. The code for that is below. There is a lot going in it. So, take a look and we'll talk about it below when you're ready.

<cffunction name="onMissingMethod" access="public" returntype="component"
         hint="Secret sauce that makes (get[thing]ByID(thingID),getNew[thing](),get[thing]Gateway()) method calls work.">

   <cfargument name="missingMethodName" type="string" />
   <cfargument name="missingMethodArguments" type="struct" />
   
   <cfset var table = "" />
   <cfset var argsList = "" />
   <cfset var args = {} />
   
   <cfif left(missingMethodName,6) EQ 'getNew'>
   
      <!--- New empty record --->
      <cfset table = mid(missingMethodName,7,len(missingMethodName)) />
      <cfreturn getBean(table).init() />
      
   <cfelseif left(missingMethodName,3) EQ 'get' AND right(missingMethodName,4) EQ 'ByID'>
      
      <!--- Record object loaded from DB --->
      <cfset table = mid(missingMethodName,4,len(missingMethodName)-7) />
      <cfset argsList = StructKeyList(missingMethodArguments) />
      <cfif listLen(argsList) GT 1>
         <!--- Multiple Arguments: just pass them through --->
         <cfset args = missingMethodArguments />
      <cfelse>
         <cfif argsList EQ "1">
            <!--- Single Positional Argument: Try to create PK by convention --->
            <cfset args[getPKForTable(table)] = missingMethodArguments[argsList] />
         <cfelse>
            <!--- Single Non-Positional Argument: Pass it through --->
            <cfset args[argsList] = missingMethodArguments[argsList] />
         </cfif>
      </cfif>
      <cfreturn getBean(table).init(argumentCollection=args).load() />
      
   <cfelseif left(missingMethodName,3) EQ 'get' AND right(missingMethodName,7) EQ 'Gateway'>
      
      <!--- Table Gateway Object --->
      <cfset table = mid(missingMethodName,4,len(missingMethodName)-10) />
      <cfreturn getGateway(table) />
      
   <cfelse>
      
      <cfthrow type="dbService.UnhandledMissingMethod"
             message="Method (#missingMethodName#) was called on com.dealerpeak.dbService and I don't know how to handle it." />

      
   </cfif>
</cffunction>

Hope I didn't loose you. This method is using some simple string functions to determine what type of object the caller wants back and for what table. Then it makes calls to utility methods that do the work. For example, say I have a handle on an instance of the dbService in a variable called 'dbService'. If I call this code "user = dbService.getNewUser()", then the onMissionMethod calls "<cfreturn getBean(table).init() />". And getBean() looks like this.

<cffunction name="getBean" access="public" output="false" returntype="component"
         hint="Given a table name, return an instance of a bean. (w/ runtime CFC generation).">

   <cfargument name="Table" required="true" type="string" />
   <cfset var beanPath = getCFCPath(arguments.Table) />
   <cfset var bean = {} />
   <cftry>
      <cfset bean = CreateObject('component',beanPath)._setDBService(this) />
      <cfcatch>
         <!--- Runtime Generation --->
         <cfset generateCFCs(arguments.Table) />
         <!--- Return bean --->
         <cfset bean = CreateObject('component',beanPath)._setDBService(this) />
      </cfcatch>
   </cftry>
   <cfreturn bean />
</cffunction>

I'm sure you can guess what the other utility methods for Gateways and DAOs look like based on that. You can also see how the runtime generation of CFCs occurs. Also, each object instantiated, gets a pointer back to the dbService.

Early on I decided that I wanted all of the SQL in the Gateways and DAOs. But, I wanted the beans to expose an Active Record like API for persistence operations. So each base bean (that will always be over-written when regenerated) has load(), save(), and delete() methods. Take a look below at how one of those works.

<!--- Convenience method so bean can save itself --->
<cffunction name="save" access="public" returntype="component" output="false"
         hint="Convenience method so bean can save itself. Calls out to dbService.">

   <cfargument name="LoadAfterSave" type="boolean" required="false" default="true" />
   <cfset variables.dbService.getDAO(variables.TableName).save(this,LoadAfterSave) />
   <cfreturn this />
</cffunction>

I won't go any farther into the details of how I customized the generated CFCs. I'll let you do your own exploration there. It has been a very iterative process. And I'm still tweaking my generator templates.

I hope you get some good ideas from this series. Please remember, this is only one way of doing it. Work politics forced me to do it this way. But, now, I'm glad that I got the chance. In the future when I'm using Transfer or Hibernate, I will appreciate how much work went into making them so flexible and powerful.

SQL Server - Insert Not Working

Just wanted to share a bit of fun I had yesterday. (Actually it wasn't very fun). I had a cfquery that was supposed to copy "Template" records linked to one entity inserting them linked to another entity. So, the file was using an Insert/Select. Never seen one? OK, it looks like this.

<cfquery datasource="#request.DSN#">
      INSERT INTO
         [Template]
         (
            TemplateName,
            OwnerID,
            ...
         )
      SELECT
         TemplateName,
         <cfqueryparam value="#attributes.Target_OwnerID#" cfsqltype="cf_sql_integer">,
         ...
      FROM
         [Template]
      WHERE
         OwnerID = <cfqueryparam value="#attributes.Template_OwnerID#" cfsqltype="cf_sql_integer">
         ...
   </cfquery>

So, we can all agree that insert/select is a great thing. However, this particular one was not working (on the live server). I tried the normal things. I cut the SQL from the .cfm and pasted it into SQL Server Management Studio (SSMS), replaced the params with the values I knew it would be using and ran it (inside of a transaction, of course). Guess what? It worked. So, it works running in SSMS but, not in a cfquery. Hmmm... So, next, I added an "output" clause to the insert/select. That way I could see any records it would be inserting, just to make sure it really wasn't inserting them. Then I added a cfdump that would only run for me. "What's an output clause?" you say. Here I'll show you.

<cfquery name="insTemplates" datasource="#request.DSN#">
      INSERT INTO
         [Template]
         (
            TemplateName,
            OwnerID,
            ...
         )
      OUTPUT
         INSERTED.*
      SELECT
         TemplateName,
         <cfqueryparam value="#attributes.Target_OwnerID#" cfsqltype="cf_sql_integer">,
         ...
      FROM
         [Template]
      WHERE
         OwnerID = <cfqueryparam value="#attributes.Template_OwnerID#" cfsqltype="cf_sql_integer">
         ...
   </cfquery>
   <cfdump var="#insTemplates#" label="insTemplates" />

I ran that and guess how many records were inserted... 0. At this point I'm a bit frustrated. But, it begins to dawn on me that I've seen similar behavior before from SQL Server. There are time when the SQL Server will cache a bad plan for some SQL. In these cases, you can either have the database flush it's entire plan cache or you can alter the SQL enough to force it to make a new plan for your SQL. So, I added "AND 1=1" to my "where" clause.

Guess what? It works now.

This is a fairly edge case. I have not seen it very often. But, if you are positive that a cfquery is not returning what it should, before you jump out of your office window, try adding something innocuous to your "where" clause.

If it does work, feel free to send some happy thoughts my way and curse MS under your breath. :-)

I'm an Adobe UG Co-Manager!

As part of a new Adobe initiatives user groups are supposed to have co-managers. So, I will be the new co-manager for the PDXRIA group. The title feels a little like "Assistant to the Regional Manager" ;-). And in more exiting news, Simeon Bateman will be stepping back up as the main manager.

We will both do our best to provide engaging presentations and valuable opportunities for networking. For more details see Simeon's blog.

P.S. I'm going to try and blog more often.

java.lang.ref.SoftReference in CFML

So, one of the coolest things I learned at the conference was about how Transfer does it's caching. Part of that is using the Java SoftReference class. Soft reference objects are cleared at the discretion of the garbage collector in response to memory demand. Let me show you what I mean.

<cfloop query="OrderedUserQry">
   <cfif NOT structKeyExists(application.UserCache, OrderedUserQry.UserIdentity)>
      <cfset user = dbService.getUserByID(OrderedUserQry.UserID) />
      <cfset application.UserCache[OrderedUserQry.UserIdentity] = user />
   </cfif>
   <cfset application.maxUserInt = OrderedUserQry.UserIdentity />
</cfloop>

This code mimics a caching mechanism that would put a hard reference in the application.UserCache to a "User" object. I just ran this repeatedly this morning until CF would not respond with anything but "java.lang.OutOfMemoryError". Fun yeah?

Change that code to this:

<cfloop query="OrderedUserQry">
   <cfif NOT structKeyExists(application.UserCache, OrderedUserQry.UserIdentity)>
      <cfset user = dbService.getUserByID(OrderedUserQry.UserID) />
      <cfset application.UserCache[OrderedUserQry.UserIdentity] = CreateObject('java','java.lang.ref.SoftReference').Init(user) />
   </cfif>
   <cfset application.maxUserInt = OrderedUserQry.UserIdentity />
</cfloop>

And you can run it all day long. I also created a page to go through the cache and get rid of keys that the soft-referenced object had been GCed. With help from SoftReference, the JVM was able to reclaim memory as needed. So, after I got to about 100,000 users having been put into cache, I ran the culling page and the cache only had arount 18,000 keys left.

<!--- Culling Code --->
<cfloop list="#StructKeyList(application.UserCache)#" index="key">
   <cfset user = application.UserCache[key].get() />
   <cfif NOT structKeyExists(variables,'user')>
      <cfset structDelete(application.UserCache,key) />
   </cfif>
</cfloop>

I definitely will be using this in caching systems in the future. There may be cases where you want a hard-reference cache. But, there are plenty more I think, where it would be fine if some objects in your cache were GCed to give the JVM the memory it needs.

cf.Objective() 2008: From Procedural to OO - Dan Wilson

One of the keywords here is "pragmatism". I know from personal, continuing experience that it can be a daunting proposition to move an application from procedural to OO.

Things you would want to consider when refactoring are in-house skill-sets, and problem spots in your application. If you have a bunch of people that are very procedural programmers, you may be wasting your time with a re-factor. You're team still has to be able to work on the code right? But, if people in your shop have some OO experience or are game to learn, then it's a good idea. Once you get started you want to ask yourself, "Where are our problem spots?". You already need to work on those spots anyway, so it's an efficient choice to start there in your OO refactor.

Always use version control! Even if you work alone! Seriously, however long it takes you to set up your version control repo of choice, it will be time well spent. You will need that version history while re-factoring.

Dan, talked some on patterns. There was a liberal sprinkling of pragmatism when talking about patterns too. Use them where they actually solve a problem and to the extent that you need them. Don't slavishly apply a pattern verbatim! I won't go into detail on the patterns. However, I'd like to mention, the first one (MVC) doesn't even require OO. However, it would be a good first step to get you closer to OO. Once you have separated the code that deals with the data (Model) from the code that displays stuff to the user (View) and the code that wires those together (Controller), then you're ready to see what can be put in Objects.

This was a talk with a lot of practical code examples. So, I think it may lose a bit in the translation.

Update: Dan was kind enough to send me a link to the presentation material. Get it here.

More Entries

BlogCFC was created by Raymond Camden. This blog is running version 5.6.002.