Browse DevX
Sign up for e-mail newsletters from DevX


Build ColdFusion Apps that Last with Mach-II : Page 2

The Mach-II framework has only been around a short time, but it is shaping up to be the most important Web architecture around that supports ColdFusion. Learn how Mach-II can help you build Web apps that are stronger, more organized, and easier to maintain.

Intro to Mach-II
Mach-II is an object-oriented redesign of the Fusebox 4/MX framework the preceded it. The Fusebox framework is a giant parsing machine that intelligently compiles smaller application scripts (and functionality) into larger scripts (and business logic). Mach-II introduces ColdFusion Components (CFCs) into the Fusebox equation: Mach-II's object model is built from CFCs, and Mach-II asks application builders to encapsulate their logic in CFCs as well. The results are wonderful: No more spaghetti ColdFusion applications with long scripts everywhere and tightly coupled application and interface code.

How does Mach-II work? Let's walk through the development of a sample application, a Web-based application to randomly generate band names. To start with, we'll need to download the Mach-II framework and place it in the ColdFusion wwwroot (see the Resources section, left column). Next, create a directory structure as follows:

/wwwroot/MachII/    <-- MachII framework installation
The index.cfm file should contain these lines, which basically just point to the Mach-II framework and set a few global values:

<cfset MACHII_CONFIG_PATH = ExpandPath("./config/mach-ii.xml") />
<cfset MACHII_CONFIG_MODE = 1 />
<cfset MACHII_APP_KEY = GetFileFromPath(ExpandPath(".")) />
<cfinclude template="/MachII/mach-ii.cfm" />
(The downloadable Mach-II sample application framework contains all this for you, but you can use the code distributed with this article as well. See left column.)

Just to get it out of the way, let's write the actual business logic of the application here, which consists of a single CFC, BandNameGenerator, with a single method, generateName(), that returns a CF List of band names based on the type of music the band plays. The first and last lines of the method are as follows:

<cffunction name="generateName" returntype="array">
    <cfargument name="musicType" type="string" required="true" />

    <cfif musicType EQ "heavy metal">
        <cfset adjectives = "Rabid,Sudden,Rancid,Maoist,Flaming">
        <cfset nouns = "Meats,Wombats,Skulls,Insecticide">
    <cfelseif musicType EQ "folk">
    <!--- . . . etc --->
    <cfset namesArray = ArrayNew(1) >
    <cfloop index="i" from=1 to=3>
        <cfset namesArray[i] = "The " & 
            & " " & ListGetAt(nouns, RandRange(1,ListLen(nouns))) >
    <cfreturn namesArray />
I left out a few lines from the middle, but the idea behind this application should be clear: Based on the music type, return a list of randomly generated band names.

So much for an application with a back end. The front end consists of two pages (or "views"), one page consisting of an HTML form where the user selects the music type, and one page where the user gets the results. That's all there is to it.

Here's the HTML from the selection page:

<p>What type of music does your band play? </p>

<form action="index.cfm?event=selectMusicType" 
    <select name="musicType">
        <option value="folk">Folk music</option>
        <option value="pop">Pop</option>
        <option value="heavy metal">Heavy metal</option>
    <input type="Submit"/>
And here's the HTML from the results page, which iterates through an array of band names stored in the request scope at request.names:

<p>Suggested band names are: </p>
<cfloop index="i" from="1" to="#ArrayLen(request.names)#" >
	<li> <cfoutput>#request.names[i]# </cfoutput> </li>
Two pages, one method call, all straightforward ColdFusion code.

Enter Mach-II
Now for the Mach-II part. No matter how small your Mach-II application, you will need to use at least four elements of the Mach-II architecture: "events," "event-handlers," "listeners," and "page-views."

A Mach-II "event" is something that happens in the application, and by default that event is announced in the URL using a parameter named event, as in:

For our application, the big event is selectMusicType, which you can see in the code from the HTML <form> action attribute above: index.cfm?event=selectMusicType. This event is sent when a user selects a music genre by submitting the HTML form.

Every event has an "event-handler," code that responds to the event and performs the needed action. In this case, our event-handler for selectBandType will want to do two things: 1) get a list of band names by calling generateName() above, and 2) display an interface that shows the results.

Next are Mach-II "listeners," code that is notified by an event-handler when a particular type of event happens. Finally, there are "page-views," which are the ColdFusion scripts that display HTML. To summarize the process: An event occurs, the event-handler looks up the correct listener to respond to the event, and then selects a page-view to display. In my music selection sample, the user selects a music type, the framework calls the generateName() method, and the results are sent to the browser by a ColdFusion script.

Thanks for your registration, follow us on our social networks to keep up-to-date