|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
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
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.
Component metadata can also be introspected programmatically using the
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#">