|
Korson.us |
CFCs (ColdFusion Components) Tutorial |
ColdFusion Web Service Tutorial |
|
ColdFusion Code |
Cfchart |
XML |
Responsive Web Design |
JavaScript Tutorial |
.NET
|
Topics Include
|
Understanding Objects in CFCs (ColdFusion Components) I 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
Seven Ways to Instantiate and Invoke 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. For these examples below, I mapping the directory (in the ColdFusion Administrator) where I keep all my CFCs (ColdFusion Components). The directory name is cfcs. Using the dot notation, I can reference the cfcs easily. This is further explained below.
(Preferred)
1 a. <!––– Instantiating the User.cfc (ColdFusion Component) –––> <cfset myUserCfcObject = createObject("component","cfcs.User")> To clarify the line of code above, "cfcs." is my mapped directory location where I keep all my CFCs (ColdFusion Components), like the User.cfc. In this case, the createObject("component","cfcs." will always remain the same. The only thing I have to change is the name of the cfc that I am instantiating and without the .cfc extension. More examples include... <!––– Instantiate the Employee.cfc that is in my cfcs directory –––> <cfset myEmployeeCfcObject = createObject("component","cfcs.Employee")>
<!––– Instantiate the Department.cfc that is in my cfcs directory –––>
<cfset myDepartmentCfcObject = createObject("component","cfcs.Department")>
<!––– Instantiate the Security.cfc that is in my cfcs directory –––>
<cfset mySecurityCfcObject = createObject("component","cfcs.Security")> To continue with my first example, I created an object (or instance) of the User.cfc. Now all the methods within the User.cfc are available to me by invoking my new object I named myUserCfcObject. For example, lets assume that I had a method within the User.cfc named getUser that required only one argument to be passed in, the user id. I would invoke the getUser method by simply typing... <!––– Invoking the getUser method wihtin the User.cfc which returns a query –––> <cfset queryThisUser = myUserCfcObject.getUser(session.UserId)> <cfdump var="#queryThisUser#" label="The users information."> The method returns a query so I named my new variable queryThisUser. This is similar to writing... <cfquery name="queryThisUser" datasource="DSN"> SELECT * FROM userTable WHERE user_id = '#session.UserId#' </cfquery> <cfdump var="#queryThisUser#" label="The users information."> 1 b. (Adequate - same code as above in cfscript) <cfscript> // Instantiate the User CFC (ColdFusion Component). myUserCfcObject = createObject("component","cfcs.User"); // Invoke the getUser Method. queryThisUser = myUserCfcObject.getUser(session.UserId); </cfscript> <cfdump var="#queryThisUser#" label="The users information."> Also see the User CFC (ColdFusion Component) below under Creating a CFC (ColdFusion Component).
<cfinvoke component = cfcs.User" method="getUser" returnVariable="queryThisUser">
<cfinvokeargument name=userId" value="#session.UserId#"> <cfdump var="#queryThisUser#" label="The users information.">
<cfobject component="cfcs.User" name="myUserCfcObject">
<cfinvoke component="#myUserCfcObject#" method="getUser" returnVariable="queryThisUser"> <cfinvokeargument name="userId" value="#session.UserId#"> <cfdump var="#queryThisUser#" label="The users information.">
http//example.com/cfcs/User.cfc?method=getUser&userId=#session.UserId#
<form name="myForm" action="http//example.com/cfcs/User.cfc" method="post">
<input type="hidden" name="method" value="getUser"> <input type="text" name="userId"> Please enter the User Id <input type="submit" name="submit" value="Get User Information"> </form>
<!––– Instantiate the User.cfc (ColdFusion Component) & invoking the getUser Method –––>
<cfscript> myUserCfcObject = createObject("webservice","http://example.com/cfcs/User.cfc?wsdl"); queryThisUser = myUserCfcObject.getUser(session.UserId); </cfscript> Or assuming that you registered the web service under an alias name as "userWebService", you don´t have to remember the long WSDL path. <cfinvoke webservice ="userWebService" method="getUser" returnVariable="queryThisUser"> <cfinvokeargument name="userId" value="#session.UserId#" /> </cfinvoke>
Creating a CFC (ColdFusion Component) & <cfscript> example This updateUser Method (User.cfc example below) might be a little more complex for the beginner programmer but lets discuss. I created an update method within my user component. In reality, I should have one insert (or create) method, one update method; and, one delete (or hide) method for my User.cfc below.
Working with ColdFusion Structures & Security for SQL Injection Notice above (User.cfc) in my updateUser method, I am passing a ColdFusion structure. Using this design, I can do all my updates to user table through this one component method. 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. Second, 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 (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: first_name = ´Eric´, last_name = ´Korson´ assuming the submitted form contained first_name and last_name; and, I entered Eric Korson. Last, If I wanted to make this method a web service, I would only need to specify access=remote" above. Improving Performance Using CFCs (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 within your webpage. Would you consider ColdFusion to be an object oriented language? CFML is not an object-oriented language, why? Objects are simply reusable application bits. They are black boxes, or magical things that do stuff, whatever you define that stuff to be. Objects often contain not just code (like CFCS) but also data, allowing data and any code that accesses it, to be cleanly encapsulated. Objects usually have multiple entry points (methods). They provide a mechanism to automatically run initialization code regardless of the entry point (a constructor). Objects can be adapted and modified, leveraging existing code without actually modifying (and potentially breaking) any of it in the process (inheritance). 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, by passing in the required arguments, for example...
<!––– Instantiate the User.cfc (ColdFusion Component). –––>
<cfset myUserCfcObject = createObject("component","cfcs.User")> <!––– Invoking the updateUser Method within the User.cfc component. –––> <cfset myUserCfcObject.updateUser(session.UserId, newPhoneNumber)>
|