Web DevCenter
oreilly.comSafari Books Online.Conferences.
MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA

Sponsored Developer Resources

Web Columns
Adobe GoLive
Essential JavaScript
Megnut

Web Topics
All Articles
Browsers
ColdFusion
CSS
Database
Flash
Graphics
HTML/XHTML/DHTML
Scripting Languages
Tools
Weblogs

Atom 1.0 Feed RSS 1.0 Feed RSS 2.0 Feed

Learning Lab






Programming ColdFusion MX, 2nd Edition

Top Ten Tips for Developing ColdFusion Components

by Rob Brooks-Bilson, author of Programming ColdFusion, 2nd Edition
09/24/2003

ColdFusion Components, or CFCs, as they are more commonly referred to, are a new construct introduced in ColdFusion MX and greatly enhanced in version 6.1. They allow you to move from totally procedural development to a more object-oriented approach. Because this represents a new paradigm for many ColdFusion developers, and the existing literature on CFCs is still in the early stages, I've assembled a list of tips to keep in mind when developing with CFCs.

I hesitate to call these tips best practices, as something better always seems to come along, but I don't hesitate to recommend them as ways to enhance your CFC development practices.

1. Create an init() Method

Since ColdFusion MX doesn't support constructors the way many object-oriented languages (such as Java) do, one technique that's being adopted by ColdFusion developers is to use an "init" method to act as a default constructor, initializing a CFC instance. Developers can use the init() method to initialize constants to be used by the component, as well as to return an instance of the component back to the calling page. A simple init() method in a CFC might look something like this:

<cfcomponent displayname="shoppingCart">
    
  <cffunction name="init" access="public" output="no" returntype="shoppingCart">
    <cfargument name="cartID" type="UUID" required="yes">
    <!--- Putting the variable in the variables scope makes it available to
          the init() method as well as all other methods in the CFC and to
          the "pseudo-constructor area --->
    <cfset variables.cartID = arguments.cartID>
    
    <cfreturn this>
  </cffunction>
  
  ...
</cfcomponent>

This init method takes a single argument, cartID. It then assigns the value of arguments.cartID to a variable in the variables scope called cartID. Variables in the variables scope are available anywhere in the CFC, meaning any method in the CFC can use the variable.

To call that init() method from a .cfm page (typically your Application.cfm page), you can use a technique called method chaining:

<cfset myCart = 
       createObject("component","shoppingCart").init(cartID=createUUID())>

Alternatively, you can also use the cfinvoke tag to do the same thing:

<cfinvoke 
  component="shoppingCart" 
  method="init" 
  returnvariable="myCart" 
  cartID="#createUUID()#" />

If you want to avoid having to manually call the init() method upon instantiation of the CFC, you could make the call within the "constructor" area of the CFC. The constructor area automatically runs whenever you instantiate a CFC:

<cfcomponent displayname="shoppingCart">

  <!--- call init() automatically when the CFC is instantiated --->
  <cfset init()>
    
  <cffunction name="init" access="public" output="no" returntype="shoppingCart">
    <cfargument name="cartID" type="UUID" required="no" default="#createUUID()#">
    <!--- Putting the variable in the variables scope makes it available to
          the init() method as well as all other methods in the CFC and to
          the "pseudo-constructor area --->
    <cfset variables.cartID = arguments.cartID>
    
    <cfreturn this>
  </cffuntion>
  
  ...
</cfcomponent>

Regardless of which technique you use, the end result is an efficient way to create an instance of a CFC while setting initialization parameters that can be used throughout the CFC.

Related Reading

Programming ColdFusion MX
Creating Dynamic Web Applications
By Rob Brooks-Bilson

2. Make the Most of Auto-Generated Documentation

Well-documented code is something all developers aspire to, but often fail to deliver on. The engineers at Macromedia have made documenting CFCs as painless as possible by making CFCs self-documenting. Even if you put no effort whatsoever into commenting your CFC code, ColdFusion MX, as well as both Dreamweaver MX and Flash MX, are able to automatically generate basic documentation for each CFC. For developers willing to invest a little time up front (see the next tip), this auto-generated documentation can be quite rich.

All of the various tag attributes used by the component tags, along with any user-defined attributes, are available as component metadata. This metadata exists as a ColdFusion structure that can be used for all sorts of purposes. ColdFusion MX comes with a tool called the CFC Explorer that you can use to introspect any CFC on your ColdFusion server. To use the CFC Explorer, simply fire up a browser and enter the URL to the CFC you want to introspect. You'll need to enter the RDS password for the server to use the CFC browser. The CFC Explorer displays a nicely formatted screen containing the component's metadata, including the component's name, path on the server, properties, and methods.

If this isn't a viable option (if you are in a shared hosting environment, for example), you may want to look into a tool called the CFCRemoteDocumenter, by Nathan Dintenfass. The CFCRemoteDocumenter is a CFML file you can include in your CFCs. It contains several methods for generating a nicely formatted page containing everything you ever wanted to know about a CFC. In fact, you might find it even more useful than the CFC Explorer that comes with ColdFusion MX. You can download a copy of the CFCRemoteDocumenter, along with documentation and examples, from Nathan's web site.

You can use another tool included with ColdFusion MX called the Component Browser to list all packages and CFCs hosted on your server. To launch the Component Browser, open your web browser and point it to localhost/cfide/componentutils/componentdoc.cfm.

As with the CFC Explorer, you'll be presented with a login screen; you'll need to enter the RDS password for your ColdFusion MX server before being able to proceed.

3. Introspect a CFC Programmatically

Component metadata can also be introspected programmatically using the getMetaData() function. When you use getMetaData() within a CFML page, it returns a structure containing metadata such as properties, methods, and parameters for the specified CFC. When used within a CFC, pass the this scope to GetMetaData() and it returns all of the metadata for the component.

Here's a piece of code that displays the metadata for the CFC Explorer component, which ships with ColdFusion MX:

<cfscript>
myObj = createObject("component", "cfide.componentutils.cfcexplorer");
metadata = getMetaData(myObj);
</cfscript>

<h2>Metadata for the CFC Explorer</h2>
<cfdump var="#metadata#">

Pages: 1, 2, 3

Next Pagearrow