What a component really is

an article added by: Steve Maye at 04282008


In: Categories » Computers and technology » Programming » What a component really is

Components are perhaps the most eagerly anticipated new feature of ColdFusion MX—and for good reason. Components not only offer a new and better way to write ColdFusion code, but they also enable ColdFusion to expose its functionality to the outside world through Web services and also make developing Flash MX applications a snap. Components bring a more object-like approach to ColdFusion, so be prepared to learn some new coding techniques. In fact, if ColdFusion is your first and only language, this object-like approach to ColdFusion programming may seem very odd and cumbersome at first, but if you stay with us through this article, you see a faster and more logical way to write maintainable applications. This article assumes that you have read about and are familiar with creating user-defined functions by using CFFUNCTION, CFARGUMENT, and CFRETURN tags. This technique really helps to communicate complex topics, and ColdFusion components is certainly one such topic. We’re using this same layered technique here, so make sure that you read the entire article, in order, from beginning to end or you may come away from it with misconceptions and incomplete code. At its most basic, a component is a collection of related user-defined functions. This is the simplest and probably the most common use of components, so we use this definition for our teaching example in the beginning of this article.

If you design it by using a few advanced principles, a component can represent a unique instance of an entity—for example, a specific company—that persists in your application and has both properties (data pertaining to that specific company) and methods (functions that the company may perform, such as updating itself in the database). More on persistent components later in this article. When referring to both components and objects, the terms function and method are interchangeable. We use each term where it seems to make the most sense or fit best with the surrounding discussion. The article takes you step by step through the process of creating two real-world components, but you need a simple example in the meantime to help explain things, so we start you out in the following lines with a basic component that simply groups together the functions related to working with a company.

You know from experience that just about every entity in our database requires basic CRUD (Create, Read, Update, and Delete) functionality and also probably requires some kind of listing capability, so you start out by creating the shell of the component that contains these functions. The name of the component is Company, so you create a file named Company.cfc that contains the first layer of the shell, as follows:

<cfcomponent>
   <cffunction name=”GetCompany”></cffunction>
   <cffunction name=”ListCompanies”></cffunction>
   <cffunction name=”CreateCompany”></cffunction>
   <cffunction name=”UpdateCompany”></cffunction>
   <cffunction name=”DeleteCompany”></cffunction>
   </cfcomponent>

Without any explanation at all, you can already tell that the preceding is a component containing five functions named GetCompany, ListCompanies, CreateCompany, UpdateCompany, and DeleteCompany—and we bet that you can guess what each of these functions is going to do! The only thing that requires any explanation is the new extension, .cfc, which specifies the file as a ColdFusion component. So far, so good. Now to give the first function, GetCompany(), an interface so it can be invoked. The next layer of the shell specifies what is returned from the GetCompany() function—a query contained in a variable named companyRec, as follows:

<cfcomponent>
   <cffunction name=”GetCompany” returntype=”query”>
   <cfreturn companyRec>
   </cffunction>
   <cffunction name=”ListCompanies”></cffunction>
   <cffunction name=”CreateCompany”></cffunction>
   <cffunction name=”UpdateCompany”></cffunction>
   <cffunction name=”DeleteCompany”></cffunction>
   </cfcomponent> 

But you need to know which company record to retrieve, so you must supply an argument containing the CompanyID, as follows:

<cfcomponent>
   <cffunction name=”GetCompany” returntype=”query”>
   <cfargument name=”CompanyID” type=”numeric” required=”yes”>
   <cfreturn companyRec>
   </cffunction>
   <cffunction name=”ListCompanies”></cffunction>
   <cffunction name=”CreateCompany”></cffunction>
   <cffunction name=”UpdateCompany”></cffunction>
   <cffunction name=”DeleteCompany”></cffunction>
   </cfcomponent>

Now you have a complete input/output interface, so you can add the business logic that does the actual work, as follows:

<cfcomponent>
   <cffunction name=”GetCompany” returntype=”query”>
   <cfargument name=”CompanyID” type=”numeric” required=”yes”>
   <cfquery name=”companyRec”
   datasource=”#Request.MainDSN#”>
   SELECT
   CompanyID,
   CompanyName,
   Address,
   City,
   State,
   ZipCode,
   Comments
   FROM
   Company
   WHERE
   CompanyID = #Arguments.CompanyID#
   </cfquery>
   <cfreturn companyRec>
   </cffunction>
   <cffunction name=”ListCompanies”></cffunction>
   <cffunction name=”CreateCompany”></cffunction>
   <cffunction name=”UpdateCompany”></cffunction>
   <cffunction name=”DeleteCompany”></cffunction>
   </cfcomponent>

At this point, you have a ColdFusion component containing one working function and shells for four more functions that you complete a little later in this article. But how do you call the GetCompany() function inside this component? You may remember from that you can call a local function inside a ColdFusion template as follows:

<cfinvoke
   method=”GetCompany”
   returnvariable=”returnVar”>
   <cfinvokeargument name=”CompanyID” value=”10”>
 </cfinvoke>

Calling a function in a component requires only one additional attribute, as follows:

<cfinvoke
   component=”Company”
   method=”GetCompany”
   returnvariable=”returnVar”>
 <cfinvokeargument name=”CompanyID” value=”10”>
 </cfinvoke>

If you specify component=”Company” in the CFINVOKE tag, ColdFusion does the following:

1. Finds the file Company.cfc.

2. Invokes the function named in the Method attribute.

3. Creates a local variable in the calling page with the name specified in the ReturnVariable attribute and the contents of what was returned by CFRETURN.

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. Building the company add action template
In this article, you create the template that puts your form data into the database. Create a file named CompanyAddAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”InsertCompany” datasource=”#Request.MainDSN#”> INSERT INTO Company( CompanyName, Address, City, State, ZipCode, Comments ) VALUES ( ‘#Trim(Form.CompanyName)#’, ‘#Trim(Form.Address)#’, ...

2. Modifying a Company in the Database
The data that CFQUERY requests from the database comes back in a result set, and the Name attribute tells ColdFusion what that result set is to be named. The SQL statement consists of three clauses: SELECT, FROM, and ORDER BY. SELECT tells the database which columns to retrieve from the database; FROM tells the database which table to retrieve those columns from; and ORDER BY tells the database how to sort the results. The result set returned from CFQUERY contains multiple rows of data, and each row has multiple columns. It would ...

3. Building the company edit action template
The code in CompanyEditAction.cfm is like the code in CompanyAddAction.cfm, but the edit action updates rather than inserts. Create a file named CompanyEditAction.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfquery name=”UpdateCompany” datasource=”#Request.MainDSN#”> UPDATE Company SET CompanyName = ‘#Trim(Form.CompanyName)#’, Address = ‘#Trim(Form.Address)#’, City = ‘#Trim(Form.City)...

4. Adding a New Employee to the Database
The CFQUERY in CompanyDeleteAction.cfm uses a DELETE statement with two SQL clauses: DELETE and WHERE. DELETE tells the database the table from which to delete a record, and WHERE tells the database which record to delete. You can watch the company delete process in action. Point your Web browser to http://<yourserver>/CFMXBible/Ch02/CompanyGetDeleteForm.cfm and enter a CompanyID. (To get a valid ID, go to the company list and pick a number from the ID column.) Click Submit to see the chosen company’s informatio...

5. Modifying an Employee in the Database
The DateFormat() function around the DateOfBirth column in the codereturns the employee’s birth date reformatted according to a display mask. DateOfBirth normally comes back from the database in the following format: 2002-01-01 00:00:00.0 That format is not very user-friendly. Calling DateFormat() with a mask of “mm/dd/yyyy” returns the date as follows: 01/01/2002 This version is, of course, more natural and easy to read. The same is true for the employee edit process...

6. Removing an Employee From the Database
The user must have the capability to remove employees from the database. The employee delete process is a simple combination of techniques that you have already learned, such as retrieving a record from the database, displaying that record in a template, and so on. The first page in this process is nearly identical to the employee get edit form. Create a file named EmployeeGetDeleteForm.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <html> <head> <ti...

7. Making direct links to the forms
Say that you want to modify or delete a company. Right now, you need to remember the company’s ID, go back to the launch pad, click Company Edit, and enter the company ID, all just to get to the edit form. Wouldn’t you rather click a company in the list and go directly to the edit form? ... <table> <tr> <td><b>ID</b></td> <td><b>Name</b></td> <td><b>Address</b></td> <td><b>City</b></t...

8. Using a Custom Tag
Custom tags are reusable, developer-authored extensions to the ColdFusion language. The custom tag that you create in the following parts displays today’s date in a familiar format. Create a file named TodaysDate.cfm inside the Ch02 directory, type the code into the file’s editing window, and save the file. <cfoutput>#DateFormat(Now(), “ddd, mmm d, yyyy”)#</cfoutput> Now() returns the current date and time, and DateFormat() reformats the date. To call your custom tag, open index.cfm, a...