ColdFusion components (otherwise known as CFCs) are a powerful feature of ColdFusion MX that allows developers to introduce object oriented programming techniques into their ColdFusion skillset. These techniques have proven to be an important part of object oriented languages such as Java, C++, VB etc
A ColdFusion component is basically a collection of functions that relate to a given entity, like for example, a Customer. You could create a ColdFusion Component that is responsible for the programming logic regarding your customer records.
For example, you could create one ColdFusion component called customer.cfc that contains five functions to perform database queries. One could create a new customer record, another function could update customer records, another to retrieve, one for deleting, and one for listing your customers.
Other functions could be included too, but you get the idea. By doing this, you would have all your programming logic for customers in one place, which has got to be a good thing - you're effectively separating your presentation from your logic.
OK, so you're thinking, "I could have done that using a CFINCLUDE tag!". True, however, ColdFusion components are much more powerful than the CFINCLUDE tag.
Benefits of using ColdFusion Components
- Security: You can restrict access to a ColdFusion component and it's functions
- Performance: ColdFusion Components are faster. This is because they are compiled. Note that a ColdFusion component only has to be compilied the first time it is called. It will then be pre-compiled for all subesquent calls (unless you update the component, in which case it will compile once again)
- Extensibility: ColdFusion components can share methods with other ColdFusion components. You can also make them available to applications built in other languages by using SOAP or URL calls.
- Reusability: You can code your component so that it is a "black box" in that you can move it around without anything breaking - it's a standalone piece of code that accepts input and provides output. Also, generally, once you've called a component in a page, you can reuse it without having to call it again in that page.
- Documentation: ColdFusion Components are self documenting - they generate their own comments. This is based on the developer using the "Hint" attribute.
Example of a ColdFusion Component
This example is a very simple version of a ColdFusion Component. You can do much more but the purpose here is to give you a basic introduction to the syntax.
<cfcomponent displayname="Customer" hint="ColdFusion Component for Customers"> <!--- This function retrieves all customers from the database ---> <cffunction name="retrieveCustomers" hint="Gets all customer from the database" returntype="query"> <cfquery name="getCustomers" datasource=#APPLICATION.dsn#> select * from from Customers </cfquery> <cfreturn getCustomers> </cffunction> </cfcomponent>
Save the above code in a file called customer.cfc and you now have a ColdFusion component!
Calling a ColdFusion Component
The following code calls the above component. (Note: This code will reside in a different file to the component. You could call it say, "ListCustomers.cfm"
<cfinvoke component="components.customer" method="retrieveCustomers" returnvariable="allCustomers"></cfinvoke>
In the above example, the component has been saved into a folder called "components" so I have to use dot notation to specify the folder. This is followed by the name of the component. Method is the name of the function within the component that I want to call (in this case "retrieveCustomers"). Returnvariable is the name I want to give to the variable that is returned by this function so that I can use it within the current page.
Once you've called the component, you can then output the rest of your page.
<table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td>First Name</td> <td>Last Name</td> <td>Email</td> </tr> <cfoutput query="allCustomers"> <tr> <td>#firstName#</td> <td>#lastName#</td> <td>#email#</td> </tr> </cfoutput> </table>
That's all there is to creating, and calling a simple ColdFusion component. In the next lesson, we'll see how easy it is to turn a component into a web service.