|
Comments
|
Today's Top SOA Links
BF on CF Using ColdFusion Components Part 1
Using ColdFusion Components Part 1
By: Ben Forta
May. 30, 2002 12:00 AM
In the May issue of CFDJ (Vol. 4, issue 5) I introduced you to ColdFusion MX - the just-released ColdFusion upgrade that is nothing short of revolutionary. As I explained in that article, CFMX features lots of incredible new technologies and features. But the one I think is most important (in terms of how it will, or should, impact your development) has to be ColdFusion Components. This month (and continued next month) I'd like to explain in depth what CFCs are and how they should be used.
CFCs Are Objects, Kind Of
But first, a necessary qualification - CFML is not an object-oriented language, and CFCs don't provide all the features and functionality typically provided by OOP languages. This isn't a bad thing - actually, I think the opposite is true. Objects have too long been the exclusive property of languages and syntactical rules that are unnecessarily complicated. There's no reason for developers using rapid development languages, like CFML, not to be able to leverage this type of technology too. Thus the CFC. At their simplest, and at the risk of offending OOP purists, objects are simply reusable application bits. They are black boxes - magical things that do stuff, whatever you define that stuff to be. If this sounds a bit like custom tags, well, there are similarities, but objects typically do more than custom tags. For example, they often contain not just code (like custom tags) 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). If this all sounds a little esoteric, an example should help clarify things.
Objects Demystified
In an object world you'd have written your application very differently. You'd have created a user object - a black box that contained everything and anything you'd ever need to do with users, as well as all the database (or LDAP) access needed to perform those operations. To perform operations on a user, you'd then create an instance of the user object, perhaps specifying the user ID in the process. Then you'd be able to use methods (functions) like:
This is the kind of functionality made possible using CFCs.
Introducing CFCs
1. Create a file with a .CFC extension (this distinguishes CFCs from ColdFusion templates, which have a .CFM extension).
The four new tags are:
Creating CFCs
This is a simplified version of a file named browser.cfc. As you can see, the code is wrapped between <CFCOMPONENT> and </CFCOMPONENT> tags. The component itself has a single function, IsIE (as in "Is Internet Explorer"), and is defined and named with a <CFFUNCTION> tag. Within the function a variable named result is defined and initialized; if the browser identifier contains the Microsoft Internet Explorer identifier, that variable is then set to "yes." And finally, <CFRETURN> returns "result." This isn't a complete example yet (although it's actually workable as is), but it's a good starting point. How would you use this code? Save it as browser.cfc, and in the same directory create a test.cfm file that should contain the following: <!--- Invoke browser CFC ---><CFINVOKE> is a new tag in CFMX and one of its jobs is to invoke component methods. As seen here, <CFINVOKE> takes the name of the component (browser, minus the extension) and the method to execute. To access any returned data, the RETURNVARIABLE attribute provides the name of a variable to contain whatever the function returns. You can try running this code. If you are running IE it will tell you so, and if not it'll tell you that too.
Writing Complete CFCs
<CFCOMPONENTS> now contains four methods:
And so, each of these new functions takes an optional parameter defined with the <CFARGUMENT> tag, which can be used in two distinct ways:
1. To check for required parameters, set REQUIRED="yes"; if the specified parame-ter isn't passed, an error will be thrown. Additional validation may be performed by specifying a TYPE, in which case ColdFusion checks that the parameter exists and that it is of the correct type.
In all four functions <CFARGUMENT> defines a single parameter, browser, allowing calling code to pass any string as needed. If the parameter isn't provided, the default CGI variable is used as before. The first three functions are basically the same as the one we looked at earlier, but the fourth, identify, which provides an alternate interface to the browser identification code, warrants special mention. Instead of checking for a specific browser (as the three prior functions do), this one checks for them all and returns an ID based on which browser it is. Rather than copy all of the previous code into this new function, it simply invokes those functions directly. But it doesn't invoke them using the <CFINVOKE> tag seen previously; rather, it uses them as functions (which they are), making the syntax much cleaner and simpler. Listing 3, a revised version of test.cfm, can be used to test all the functions. It's as simple as that.
Documentation
CFCs may be introspected - a fancy way of saying that they can be asked to describe themselves. As they know all about their functions and what they do, they can fulfill this request quite admirably. Here are two ways to introspect a CFC:
1. Execute a CFC directly in your browser, providing a URL directly to it (something like http:// local-host/path/browser.cfc). ColdFusion will generate cleanly formatted documentation on-the-fly, using everything it knows about the component (including the specified HINTs).
There's more to introspection, but we're out of space for now.
Summary
Reader Feedback: Page 1 of 1
Your Feedback
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week |
||||||||||||||||||||||||||||||||||||