Untitled Document

Understanding and Using CFCs (with OOP and database interaction)

Part 1

Introduction

In this tutorial we will go over the basics of creating CFCs. This multi-part tutorial series was created mostly in part because it seemed to me there was a very limited number of articles and tutorials addressing the subject of CFC development.

I will be using a number of terms in this tutorial that are common in the world of Object Oriented Programming (or OOP). Even though CFCs do not share all aspects of objects in true OOP languages, we can benefit by using those aspects that are available.

Objects are simply a way of describing an item, or "thing". We as humans naturally think and process information in the terms of objects, often without realizing it. When I say "put a pizza in the microwave", you automatically know that pizza is a type of food, and microwave is a type of oven, or an object that has the ability to heat something.

In CFC terms, we might have a "oven" CFC. The oven has both actions and data properties to it. Some of its data properties might be "color", "brand", "size", and so on. The imporant thing to remember about out oven CFC, is that all "oven" objects share these same data properties, what makes one oven different from another, is value of those properties. Every oven also shares actions that are similar or equal. For example, all ovens have "start", "end", "open door" and so on actions to them. We call these functions or methods.

When we use our CFCs as objects, we allow for maximum code re-use by modularizing these little "things" in our code.

Although you can use CFCs for many different tasks, an excellent usage of them is to become a buffer between your page building code and the database which is what we will go over in this tutorial.

When you design a database for storing your application information, if well normalized, then your data will likely be split up into "things" as well. In our examples for this tutorial, we will deal with two objects (or things) that have corresponding database tables. Jobs and Tasks.

In this multi-part set of tutorials, we will explore ways to use CFCs to store data and interact with the database.

Before we begin, let me preface that I am writing this tutorial to the best of my knowledge as a developer of many languages over my career. All code displayed has been tested to run on ColdFusion MX 6.1 and should work as described at the time of publication. As with any language, your implementations and practicies may differ, but these are methods I have seen to be highly useful and productive in the past.

The introduction of CFCs to mankind

CFC stands for ColdFusion Component, a new and exciting feature that was added to ColdFusion MX (also reffered to as version 6).

Defining the finished product

When we are done, we will want to be able to interact with records in our database with a layer of abstraction provided by our CFCs. None of our front end code will have to query the database, nor will it have to keep track of database columns and keys. This effectively puts all of the code for our data interaction inside our CFCs, encapsulating them!

To further illustrate our end result, we will be able to use the following snippet to insert a new job into the database:

Dont get caught up in the syntax, we will go over all of it soon in the following tutorials. The following is just to illustrate the simplicity of data interaction of our end result.

<cfscript>
     // create a job object
     newJob = createObject("component", "job");
     // set the name property of it
     newJob.name = "My new project";
     // save it to the database
     newJob.insertJob();

</cfscript>

And perhaps to search for a particular job and change it in the database, we might use something such as this:

<cfscript>
     // create a job search object
     jobSearch = createObject("component", "job");
     // set the name property of it, which will be our search criteria
     jobSearch.name = "My%";
     // perform search and assign results to a new var (array)
     searchResults = jobSearch.searchJobs();

     
     // our searchResults var now holds an array of job objects found

     // set the new name property
     searchResults[1].name = "My new project name ";
     // update the database with our changes
     searchResults[1].updateJob();

</cfscript>

Although the above syntax may be a bit confusing right now, you can immediately see that interacting with the database is much simpler and less code using our data object CFCs.

We have also modularized our database interaction into the CFC only, so we do not have to hunt down multiple pages to make changes if our data model changes!

Setting the stage

For this tutorial, we will examine a simple application that will manage jobs and tasks. In our database we have two tables:

Jobs Table
Field Name DataType
job_id int, identity
display_name varchar

Tasks Table
Field Name DataType
task_id int, identity
job_id int, fk to jobs.job_id
display_name varchar
date_started datetime
date_completed datetime
assigned_to varchar

 

Our CFCs

We will be creating two CFCs for interacting with our data.

task.cfc

This CFC will manage the data for our tasks table. It should have the following functionality:

  • initialize the data the task holds / properties of the component
  • insert a new task
  • update an existing task
  • search for a specific task

job.cfc

This CFC will manage the data for our jobs table. It should have the following functionality:

  • initialize the data the job holds / properties of the component
  • insert a new job
  • update an existing job
  • search for a specific job
  • get any tasks associated with our job

In part two, we will begin creating our first CFC.

About This Tutorial
Author: Nate Nielsen
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX
Total Views: 26,592
Submission Date: September 13, 2004
Last Update Date: June 05, 2009
All Tutorials By This Autor: 9
Discuss This Tutorial
Advertisement

Sponsored By...
Powered By...