Exercise 4: Writing structured, reusable code

Generally, it is good coding practice to separate business logic from the display. The ColdFusion page that you just created contains both business logic (the database query) and presentation (the output code block). To separate them, you put the query in a ColdFusion component (CFC). Doing so separates business logic from presentation; it also makes it easy to reuse the query anywhere in your application. For more information, see Building and Using ColdFusion Components in ColdFusion MX Developer’s Guide.

To move the query to a CFC:

  1. Create the CFC file.
  2. Copy the query to the CFC.
  3. Call the method that contains the query.

Creating the CFC file

ColdFusion components (CFCs) are special files saved with the filename extension .cfc. They can contain data and functions. Within CFCs, functions are referred to as methods. Actions that you want ColdFusion to perform, such as querying a database, are contained in component methods. One CFC can contain many methods. Each method in a CFC can return only one variable. The following is the general syntax of a CFC:

<cfcomponent>
	<cffunction name="firstMethod">
		 <!--- CFML code for this method goes here. --->
	</cffunction>
	<cffunction name="secondMethod">
		 <!--- CFML code for this method goes here. --->
	</cffunction>
</cfcomponent>

To create the CFC file:

  1. Create a directory named components as a subdirectory of the my_app directory.
  2. Open a new blank file.

    Note: If you are using Dreamweaver, select Dynamic page in the Category list, and select ColdFusion Component in the Dynamic Page list.

  3. Enter the following code, or do the steps listed in the Let Dreamweaver do it section.
    <cfcomponent displayName="Get Trips" hint="Get trip information">
    	<cffunction name="basicList" displayName="List all trips" 
    	hint="List trips in same order as in table" access="public"
    	returnType="query" output="false">
    
    		<cfreturn>
    	</cffunction>
    </cfcomponent>
    
  4. Save the file as gettrips.cfc in the components directory.

Let Dreamweaver do it

Dreamweaver lets you create a ColdFusion component without having to enter the code.

To create a CFC in Dreamweaver:

  1. Click the Components tab.
  2. Click the + button.
  3. In the Display name text box, enter Get Trips.
  4. In the Hint text box, enter Get trip information.
  5. In the Name text box, enter gettrips.
  6. Click the Browse button, and make the components directory the current directory.
  7. In the Section list, click Functions.
  8. Click the + button.
  9. In the Name text box, enter basicList.
  10. In the Display name text box, enter List all trips.
  11. In the Hint text box, enter List trips in same order as in the table.
  12. Select query as the Return type.
  13. Click OK.
  14. Save the file as gettrips.cfc in the components directory.

Copying the query to the CFC

To copy the query to the CFC, you copy the CFML to the CFC, between the opening and closing cffunction tags.

To copy the query to the CFC:

  1. Highlight the following code on the triplisting.cfm page:
    <cfquery name="TripList" datasource="CompassTravel">
    	SELECT trips.tripName FROM trips
    </cfquery>
    
  2. Cut the highlighted code and copy it to the gettrips.cfc page so that it appears as follows:
    <cfcomponent displayName="Get Trips" hint="Get trip information">
    	<cffunction name="basicList" 
    		displayName="List all trips" hint="List trips in same order as in table"
    		access="public" returnType="query" output="false">
    		<cfquery name="TripList" datasource="CompassTravel">
    			SELECT trips.tripName FROM trips
    		</cfquery>
    		<cfreturn>
    	</cffunction>
    </cfcomponent>
    
  3. Modify the code by adding the following text so that the method returns the results of the query to the triplisting.cfm page:
    <cfreturn TripList>
    
  4. Save the gettrips.cfc file.

Calling the query method

To perform the query that is now in a method in a ColdFusion component, you have to call (invoke) the method. To do so, you can use the cfinvoke tag. Within the cfinvoke tag, you specify the name of the ColdFusion component, the method to call, and the query to return to the calling page. The name of the component includes the package, "cfdocs.getting_started.my_app.components." The package looks very similar to the path, except that it contains periods instead of slashes. Like a path, it specifies the location of the component.

To invoke the method:

  1. Go to the top of the triplisting.cfm file.
  2. Enter the following code, or do the steps listed in the Let Dreamweaver do it section.
    <cfinvoke 
     component="cfdocs.getting_started.my_app.components.gettrips"
     method="basicList"
     returnvariable="TripList">
    </cfinvoke>
    
  3. Save the file.
  4. View the triplisting.cfm page in a browser and notice that the page lists the trip names, just as it did previously.

Let Dreamweaver do it

Dreamweaver lets you invoke a method in a CFC without having to write the code.

To invoke the method using Dreamweaver:

  1. Click the Components tab in the Application panel.
  2. Click the + button next to cfdocs.getting_started.my_app.components.
  3. Click the + button next to gettrips.
  4. Select query basicList() and drag it to the top of the triplisting.cfm file.
  5. Change the value of the returnvariable to be the name of the query, TripList, as follows:
    returnvariable="TripList"
    
  6. Return to Step 3 in the To invoke the method: procedure.