Welcome!

ColdFusion Authors: Greg Ness, Liz McMillan, Pat Romanski, Andreas Grabner, David Strom

Related Topics: ColdFusion

ColdFusion: Article

XML and cfcPowerTools

Using CFCs

CfcPowerTools is a web GUI for generating Cold Fusion Components (CFCs). I started working with CFCs when ColdFusion MX 6 was released. Like many developers, I was intrigued as to what CFCs are and how I can use them to be a better, more productive developer. I played with CFCs following what documentation Google found for me and realized the potential CFCs offer.

I found some "best practices" documentation that suggested instance data (data that's part of the CFC after it has been instantiated) should be protected from public access. The documentation recommended the use of getter and setter methods for presenting a controlled interface to instance data. Sounds like a great idea. Who doesn't want protected data? Anyways, after a couple months of finger boning getter and setters (and fixing the subsequent errors) I wanted something better. Like any good developer, I'm lazy. So, I spent the better part of two years creating cfcPowerTools so I do not have to write getter/setter methods by hand.

cfcPowerTools has grown out of a desire to have getter/setters created for me. It has grown into an application that will:

  • generate a CFC from a database table
  • generate a database table from a CFC
  • batch generate CFCs from multiple tables
  • generate data entry form
  • generate Data Access Object (DAO) CFC
  • generate XML document containing all property Metadata
  • generate getter/setter methods
My first incarnation of cfcPowerTools put all of the metadata collected for a property into the <cfproperty> tag. A version 1 <cfproperty> looked like this:

<cfproperty elementmaxlength="255" hint="email from" position="1"
txtDefaultValueType="Simple Value" type="string" elementsize="35" name="From"
propertyform="textbox" datatype="varchar" displayName="From" required="1"
propertydisplaylevel="1">

This is not very user-friendly. A developer would not do this if he or she were writing the CFC by hand. I wanted to generate a CFC file that you could not tell if it was created by hand or by cfcPowerTools. So, I needed a better way to manage a property's metadata.

XML to the Rescue
XML seemed to be the best choice for storing the property metadata used to describe the property from a database and form's perspective (see Listing 1). Plus it allowed me to generate a <cfproperty> tag like this:

<cfproperty displayname="Article Name" name="txtArticleName" type="string">

How Does It Work?
You describe your CFC and its properties when you create a CFC with cfcPowerTools. This metadata is collected and stored in an XML document. You describe how your properties are viewed by a database table and how they are viewed by a data entry form. Each CFC has an XML document. cfcPowerTools consumes the XML and uses the information in everything cfcPowerTools does from viewing a CFC's anatomy to generating DAO, data entry form, and database table. The CFC generated by cfcPowerTools looks like a typical CFC you might create by hand. cfcPowerTools does not introduce any of the metadata into the physical CFC file (see Listing 2).

What Benefits Does an XML Document Have?
Having the metadata in an XML document has several advantages:

  • It uses an industry standard in XML. My first incarnation had metadata inside the <cfproperty> tag, which turned out to be cumbersome and awkward.
  • ColdFusion MX works seamlessly with XML.
  • It facilitated overwriting inherited property's metadata. This was a huge benefit. I can now extend a CFC and overwrite the parent's property's metadata.
  • Manual modifications of CFC property metadata. You can change the metadata without getting into cfcPowerTools.
How Does cfcPowerTools Consume the XML Metadata?
I was challenged with figuring out how to merge my metadata with the metadata ColdFusion has on a CFC. This turned out to be surprisingly easy. ColdFusion has a getmetadata() method that introspects a CFC and produces a structure containing a CFC's metadata (see Figure 1). All I had to do is loop over each property in ColdFusion's metadata structure and append the metadata from the XML document.

The included code example shows how I am pulling data out of an XML document and merge with ColdFusion's CFC metadata. The example files are as follows:

  • locations.cfc - an example CFC generated by cfcPowerTools
  • locations.xml - an example XML document with property metadata
  • mergeMetaData.cfc - example code for how I merge locations.xml metadata with ColdFusion's metadata
  • test.cfm - page displays the results of the merged data plus the original property metadata from getMetaData()
mergeMetaData.cfc
mergemetaData.cfc is a watered down example of what cfcPowerTools does. Basically, I pass in the name of the CFC file (locations) and the first thing it does is instantiate the CFC file and retrieve ColdFusion's metadata (see lines 28-31).

//if stMetaData is not known, then getMetaData()
if(structcount(arguments.stMetaData) EQ 0){
   oCFC = createobject("component",arguments.cfcName);
   arguments.stMetaData = getmetadata(oCFC);
}

More Stories By Tom Schreck

Tom Schreck is a Macromedia certified ColdFusion MX 6.1 developer. He has been working with ColdFusion since 1997. Check out www.cfcPowerTools.com for more information on cfcPowerTools.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.