CFUnited 2009: Adam Lehman - ColdFusion 9, What's New

First up was Server.cfc. However that is a bit of a misnomer. You can actually point the administrator to any CFC. That CFC simply needs to have an onServerStart() method. This should be quite a boon for getting sites that have an expensive (read "slow") first request all spun up and ready for traffic.

Next up was nested cftransaction. Not a lot to explain here. From what I saw, it should satisfy you if you ever said to yourself "I really wish I could nest transactions in CF".

Then we got introduced to cffinally/finally. I personally have never used this even in languages that I have access to it. Basically it gives you this syntax: "try { ... } catch (e) { ... } finally { ... }". If someone wants to explain an compelling use case for "finally", I would love to hear it...

And then there was cfcontinue and it was good. Seriously! I mean, I'm excited about this one and at the same time amazed it took them this long.

[More]

CFUnited 2009: Peter Bell - Requirements and Estimating

Peter looks at projects in roughly 3 categories.

  • Configuration < $8000 - free spec (just set up something already built)
  • Customization < $50,000 - paid spec (requirements gathering, setting up and customizing packages)
  • Exploration $50,000 - no spec (hard to even define scope)

Configuration

These projects are all about efficiency. You will need to simplify the specs for these types of projects. You will need to have/use configurable code to implement deliverable. That could be via something with a setting file or configuration wizard. In some cases you might use DSLs (domain specific languages). And for very simple stock types of things you can even reuse prior specification documents (copy and paste, or compile stock specs as you go).

Customization

I think here he was talking about a site that will use a lot of code that you or someone else already wrote.

[More]

Using argumentCollection And Overriding Arguments

I ran some test code the other day just to make sure that I correctly understand how arguments passed in to functions in CFML work. If you know everything there is to know about functions and arguments in ColdFusion, then feel free not to read the rest of this. But, if you're curious...

Here is the code:

<cfoutput>
   <cffunction name="ExtraArgs" access="public" returntype="string">
      <cfargument name="Arg1" required="false" type="string" />
      <cfargument name="Arg2" required="false" type="string" />
      <cfargument name="Arg3" required="false" type="string" />
      <cfargument name="Arg4" required="false" type="string" />
      <cfif structKeyExists(arguments,'Arg4')>
         <cfreturn arguments.Arg4 />
      <cfelse>
         <cfreturn 'undefined' />
      </cfif>
   </cffunction>
   <cfset args3 = {Arg1='value1', Arg2='value2', Arg3='value3'} />
   <cfset args4 = {Arg1='value1', Arg2='value2', Arg3='value3', Arg4='value4'} />
   <br/> 1) #ExtraArgs(argumentCollection=args3)#                   <!--- output = "undefined" --->
   <br/> 2) #ExtraArgs(argumentCollection=args4)#                   <!--- output = "value4" --->
   <br/> 3) #ExtraArgs(argumentCollection=args4, Arg4='SomeOtherValue')# <!--- output = "SomeOtherValue" --->
</cfoutput>

All of the arguments are specified as 'not required' with no 'default' value. The function looks for the existence of the fourth argument and either returns that or the string "undefined".

The first time it is called with the "args3" struct that omits the "Arg4" argument, it returns "undefined".

The second time it is called with the "args4" struct, which contains "Arg4", it returns "value4" (the value of "Arg4" in the args4 struct).

The third time it is called with the "args4" struct and also the "Arg4" argument in addition to that, it returns "SomeOtherValue". This, I think, is the most interesting one. With this behavior, you could store structs containing the default arguments for certain operations, and then call those operations overriding defaults as needed.

Die Spammers Die! (Painfully Please)

My blog has been absolutely hammered with comment spam lately. If any of you were subscribed to any of those comments and were forwarded the offensive spam, I am very sorry.

Fortunately, one of the bots wasn't submitting something correctly. So, instead of getting a few thousand spam comments I got a few thousand error emails. :-(

Anyway, I just took the time to add CFFormProtect to my comment form. It was super easy. There was really no excuse for me not to do this sooner. So, again, I apologize.

Just to show you how easy it is, here is the implementation code from the docs.

Put
<cfinclude template="/cfformprotect/cffp.cfm">
somewhere between your form tags.

<!--- On your processing page include the following code: --->
<cfset Cffp = CreateObject("component","cfformprotect.cffpVerify").init() />
<!--- now we can test the form submission --->
<cfif Cffp.testSubmission(form)>
<!--- The submission has passed the form test. Place processing here --->
<cfelse>
<!--- The test failed. Take appropriate failure action here. --->
</cfif>

Of course there are lots of things you can do to customize it's behavior. But, that is the basics. So please go checkout CFFormProtect for all your 'spam form submission' needs. Because, seriously, Captcha is the suck.

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.

More Entries

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