Simple Intro to CFC

Recently, my ColdFusion User Group had an introduction to CFC's lesson.  I found it difficult to understand some simple concepts.  When I'm learning things, I have to start on the simplest level and work my way up, so thats my reason for this blog in a nut shell.

I set up a very small database with 1 table ( tblTest ), 2 columns ( tstFirstName, tstLasName ) and filled it with some test data.
My goal was to have a simple input form asking for someones last name, then return the user with the first name by calling a cfc. 

Here is my cfc:
<cfcomponent>
 <cffunction name="ReturnFirstName" access="public" returntype="string" hint="This will return the first name when passed a last name">
  <cfargument name="LastName" type="string" required="Yes">
       
     <CFQUERY name="get_employees_first_name" datasource="dsnTest">
            select tstFirstName
            from tblTest
            where tstLastName = '#arguments.LastName#'
  </CFQUERY>
  <CFRETURN get_employees_first_name.tstFirstName>
 </cffunction>
</cfcomponent>

 

Here is my cfm:
<CFINVOKE component="myCFC" method="ReturnFirstName" returnvariable="foo">
     <cfif ISDEFINED('form.btnSubmit')>
         <CFINVOKEARGUMENT name="LastName" value="#Form.myinput#">
        <cfelse>
         <CFINVOKEARGUMENT name="LastName" value="">
     </cfif>
</CFINVOKE>

<body>
<cfform name="form1" action="cfcTest.cfm" preservedata="yes">
<br/>
    Hello <CFOUTPUT>#foo#</CFOUTPUT>
<br />
What is your Last Name?
  <cfinput type="text" name="myinput">
<br/>
<cfinput type="submit" name="btnSubmit">
</cfform>
</body>

 

This very simplified version a cfc makes it very easy to see what is actually happening and how it can be useful.  Now that I have created this cfc, I can send the persons last name to the cfc throughout my entire application and it will easily send me back the persons first name.  I never have to write that function again.

Here is a great tutorial that helped me understand this simple, but extremely useful function: http://tutorial80.easycfm.com/ 

del.icio.us Digg StumbleUpon Facebook Google Bookmarks DZone
| View count: 179
blog comments powered by Disqus