CFC Tutorial / ColdFusion Components By Eric Korson
|
Understanding Objects in ColdFusion ComponentsI would like to briefly discuss a few terms from the object-oriented language that you might normally use. Understanding these terms will help when working with components. Object
|
Improving PerformanceUsing ColdFusion components increases performance because they only have to be compiled the first time they are called. After compiled once, the object remains in cache for all subsequent calls to that component; and, you can re-use the object without having to call it again in that page. |
Instantiating a CFC (ColdFusion Component) |
| Typically, you create an instance of a component; and then, call a method on it, passing data in the form of arguments. There are seven different ways in which you can call a component.
1). cfinvoke tag - Instantiates a CFC and / or invokes a method on an instantiated CFC. <cfset userObject = createObject("component", "user")>2). cfobject tag - Properties must be explicity set; and, methods explicitly called. <cfobject name="userObject" component="user.cfc">3). createObject() function - Instantiates as object via cfset, cfparam; or, cfscript. Properties must be explicitly set; and, methods explicitly called (See my example below: Attaching the CFC to the application scope). <cfscript>4). URL - Invokes a component method directly via HTTP GET. The CFC file name is specified in the URL along with the method name as a URL parameter. http//example.com/cfcs/user.cfc?method=getUserDetailOnId&id=15). Form Post - The Action attribute of the HTML form or cfform tag posts directly to a CFC. The method to call must be specified by the form field. <form action="user.cfc" method="post">6). Web Service - CFCs can be consumed as web services. <cfscript>Or assuming that you registered the web service under an alias name as "userWebService"; so, you don't have to remember the long wsdl path. <cfinvoke webservice ="userWebService" method="getUserOnId" returnVarible="usersInfo">7). Flash Remoting - Flash MX animations can call ColdFusion components via Flash Remoting. This example could take a bit more of explaining; so, I am going to skip this one to move forward.
|
| In my example, I created a template named cfcObject.cfm (see below). By including the template under the OnApplicationStart() method within the application.cfc, I instantiate my CFC Objects. ColdFusion will run the onApplicationStart method the next time a user requests a page. |
application.cfc |
Creating a CFC (ColdFusion Component)Below I created an update method within my user component. In reality, I should have one insert (or create) method, one update method (my code below); and, one delete (or hide) method within my user.cfc below. Working with ColdFusion StructuresFirst, notice below in my user.cfc that I am passing a ColdFusion structure to my updateUser method. Using this design, I can do all my updates to user table through this one component function. In my example, when I pass a structure to the SQL database, it doesn't matter in what order the fields come; or, the number of fields I pass as long as I follow the SQL rules. For example, the field names in my structure must match the field names in the database; or, I would receive a column name mismatch error. Security for SQL InjectionSecond, notice within my cfscript code, as I build the valueSet within my loop valueSet = listAppend(valueSet,key...) to pass to my SQL stored procedure, I also call my sqlSafe method (application.utilityObj.sqlSafe(userStructure[key])) to strip out any unwanted characters. The possible SQL injection is handled at the component level; and, acts as another security feature towards SQL injection (among my other security features). Once executing the cfscript tag, the variable of valueSet would look like: fname = 'Eric', lname = 'Korson' assuming the submitted form contained fname and lname; and, I entered Eric Korson. Last, I wanted to add this method to my web service; so, I only needed to add one attribute, access="remote". |
user.cfc |
cfcObject.cfm |
Calling the CFC (ColdFusion Component)In my example, I created a ColdFusion structure; and, called the CFC as shown in the bottom line of code. Again, this is assumed that I submitted a form with the field names the same as my database column names. The bottom line of code calls the CFC to update the database. |
Would you consider ColdFusion to be an object oriented language, why? |
CFML is not an object-oriented language, why? For example: In ColdFusion, to perform operations on a user, you would first create an instance of the user object, and then you would be able to call a method upon it, and perhaps specifying the user ID in the process, like: • Add() In an object-oriented world, you would have written your application very differently. You would have created a user object (a black box) that contained everything you needed for the users method. The idea is that any and all user processing would happen inside the user object. You would never access user tables directly. Actually, you wouldn't even know (or care) that the data is being stored in a table. You would just invoke methods as needed, letting the code inside the object do its thing. This is the kind of functionality made possible using CFCs. |