Article

Home » Server-side Coding » ColdFusion Tutorials » Create Scalable Applications with ColdFusion Components

About the Author

Ben Davies

author_Ben-Davies Ben has been using ColdFusion to develop intranet, integration, and high-volume reporting solutions for five years. In addition to providing a consultancy service, Ben is Lead Systems Developer for Vale Inco's Goro Nickel Project. Ben lives and works in Brisbane, Australia, but enjoys any opportunity to travel.

View all articles by Ben Davies...

Create Scalable Applications with ColdFusion Components

By Ben Davies

May 23rd, 2008

Reader Rating: 9

Page: 1 2 3 Next

This article presents an introduction to ColdFusion components (CFCs). For developers seeking to take their skills to the next level, CFCs are an invaluable addition to the toolkit, allowing the adoption of a more professional approach to web application development.

Ushered in with ColdFusion MX 6, CFCs have enjoyed strong support from the developer community ever since. These days, ColdFusion 8 provides both significant and subtle improvements to the way CFCs work, which include major new features such as close Ajax integration, not to mention greatly enhanced performance.

Being able to develop using CFCs allows us to write highly maintainable and flexible ColdFusion applications and reuse more code. Even better, it also permits us to leverage great capabilities in ColdFusion, both new and existing, such as automatic web service generation and powerful Ajax tags.

This article will cover the following topics:

  • what a CFC is
  • why CFCs are useful and worth learning
  • how to write a CFC
  • how and where to start using CFCs in applications
  • an introduction to other great features in ColdFusion that use CFCs
  • tips for developers new to CFCs

Requirements

software

sample code

A set of sample code for using the sample BookClub database that shipped with CF8 is available here.

prerequisite knowledge

Users are assumed to be familiar with basic ColdFusion development, including user-defined functions -- refer to the ColdFusion documentation if you need a general refresher or specific reminder.

What Is a ColdFusion Component?

ColdFusion components (CFCs) are a way of grouping related functions and data together in the one file, regardless of where they're needed throughout our application. Some CFCs model a particular piece of data; others provide functions for accessing external data. We'll focus on the latter in this article.

When we work with ColdFusion components, we're really using a form of object oriented programming (OOP). If you haven't been involved with any OOP before, don't feel intimidated. You don't need to be familiar with the object oriented features of ColdFusion in order to read, understand, and make use of this article and ColdFusion components.

Why Are ColdFusion Components Useful?

It is possible to write sophisticated applications without components, but using CFCs in your applications help make them more maintainable and flexible while allowing greater code reuse. For anything other than a throwaway application, CFCs allow for better, more reliable, and more scalable applications.

These advantages arise from separating presentation logic from the business rules and data access logic. Code that generates direct output -- such as ordinary HTML markup and ColdFusion tags like CFFORM -- tends to vary considerably from page to page. Business and data access logic, however, tends to remain constant.

CFCs allow us to capture this business and data access logic and separate it from the page markup, in effect creating a separate presentation and business layer in our application. Changing the presentation layer is less likely to affect the business layer, and vice versa. And the changes we make anywhere are likely to be smaller, since we're no longer repeating ourselves unnecessarily. This is a good first step towards developing better architecture for our applications.

As an additional advantage, working with ColdFusion components within our applications also allows us to take advantage of some of the more powerful features of ColdFusion, such as automatic web-service generation, Ajax controls, and the application framework.

The Sample Application

Over the course of this article, we'll use a sample application to see how CFCs can be used in applications. Using this BookClub database, we'll work with a web application that, apart from tracking books and authors, also allows the user to read and post blog articles. Let's dub our web site "BookBlogs."

This web site begins its life as a traditional page-based CFML application. Nearly all ColdFusion developers start with applications such as this one. This table of excerpts from the bookblogs, members, and blogs folders should seem familiar:

/bookblogs/bookblogs/members /bookblogs/blogs
application.cfmmemberList.cfmblogEntryList.cfm
index.cfm profile.cfmblogEntry.cfm
editProfileForm.cfmnewBlogEntryForm.cfm
updateProfile.cfmaddBlogEntry.cfm/
register.cfm

Other folders in the application have a similar structure. Apart from separate include files for headers and footers (hidden away in the /bookblogs/layout folder), our application is otherwise made up of a CFM file for each page the user can visit. Each of these CFM files incorporates all the required CFML code to apply business logic, access the database, and output the results. This can be seen by examining the contents of the blogEntryList.cfm file:

<cfset pageTitle = "Blogs" />

<cfinclude template="../layout/layoutQueries.cfm">
<cfinclude template="../layout/header.cfm">

<!--- Retrieve All Blog Entries --->
<cfquery datasource="#application.dsn#" name="blogEntries">
 SELECT * FROM BlogEntries
 ORDER BY Posted DESC
</cfquery>

<h2>All Blog Entries</h2>
<table width="90%">
 <tr>
   <th>Date</th>
   <th>User</th>
   <th>Title</th>
 </tr>
 <cfoutput query="blogEntries">
   <tr>
     <td>#dateFormat(posted)#</td>
     <td>#username#</td>
     <td>#title#</td>
   </tr>
 </cfoutput>
</table>

<cfinclude template="../layout/footer.cfm">

As we can see in the figure below, the blogEntryList.cfm page queries the database for a complete listing of blog posts and displays them all in a simple HTML table. There's nothing challenging about this code, except for the fact that the same SQL statement (and therefore business rule) appears with slight variations in three pages across the application.

Firstly, the blogEntryList.cfm page displays a listing of all blog entries, as shown.

Our BlogEntryList page

As you would expect of a blog site, the homepage, or more specifically our index.cfm page, shows a list of the most current blog entries.

Our index.cfm page

And the profile.cfm page shows the blog entries for a particular author.

Our profile.cfm page

Coding such a simple SELECT statement in three places during development might not seem too arduous, but it's generally after the source code leaves our hands and must be supported that problems occur. When we return to fix a bug, or add an approval feature, integrate another data source, or simply apply the client's latest request, we find that we need to modify and test each file. Without good documentation and a simple code base, it can be problematic to even remember which CFM files we need to update.

Naturally, there is a better way: define a BlogManager ColdFusion component to capture all blog-related business rules and data access. And that's exactly what we're going to do.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links