Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

ColdFusion MX Overview – Exploring CFCs In-Depth

 Page 6 — Exploring CFCs In-Depth


Lets create another component that expands on our understanding of the first one. This time, we’re going to use the optional HINT attribute, and throw another few <CFFUNCTION> tags into the mix.

dateExample.cfc

<CFCOMPONENT HINT=”This component is used to perform very
simple date manipulation. It can tell you the name of the
current day, what yesterday was, or what tomorrow will be.
It can also tell you the day name of any valid date
that you pass to it.”>
 <CFFUNCTION NAME=”getToday”
  HINT=”Returns the name of the day today.”>
  <CFRETURN dayofweekAsString(dayofweek(now()))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getYesterday”
  HINT=”Returns the name of the day yesterday.”>
  <CFRETURN dayofweekAsString(dayofweek(dateadd(“d”, -1, now())))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getTomorrow”
  HINT=”Returns the name of the day tomorrow.”>
  <CFRETURN dayofweekAsString(dayofweek(dateadd(“d”, 1, now())))>
 </CFFUNCTION>
 <CFFUNCTION NAME=”getThisDayName”
  HINT=”Allows you to pass an arbitrary date. The function
  returns the name of the day that the given date falls on.
  Be sure to pass a valid date. For example: ’12/1/2002′.”>
  <CFARGUMENT name=”day”>
  <CFRETURN dayofweekAsString(dayofweek(day))>
 </CFFUNCTION>
</CFCOMPONENT>

 

As you may have guessed, the optional HINT attribute is used to describe what components and functions do. Not only is it easy to understand what’s going on when you read through the file, but the real advantage is that ColdFusion Server understands your hints and does useful things with them. The fancy-shmancy word for it is “introspection”. Lets take a look at introspection in action. If you request your .cfc file directly with your Web browser, ColdFusion Server will automatically create a help document for your component based on your hints and functions.

ColdFusion date Example CFC in Browser

Dreamweaver MX also has a component browser that instantly recognizes your component, understands everything that it can do, and gives drag and drop functionality to your code.

ColdFusion date Example CFC in Dreamweaver

You will find that it will make sense to create components that contain related functionality. For example, you might create a component that handles user authentication, or one that operates on products in a store. As long as your team uses the HINT attribute, when you want to find out how the CFC works, you simply load the CFC in your browser and ColdFusion explains to you how it works. Cool, huh? One other nice feature of CFCs is that they get automatically compiled as Java class methods by CFMX, which makes them much faster than custom tags or regular UDFs.

In addition to putting your .cfc files in the same directory as the calling .cfm page, you can also put your .cfc files in custom tag paths and call components in the same way. Alternatively, you can put a component in a directory that has a ColdFusion mapping. For example, if you create a ColdFusion mapping to C:components and name it “myComponents”, then you could call our dateExample component like this:

<CFINVOKE
 COMPONENT = “myComponents.dateExample”
 METHOD  = “getTomorrow”
 RETURNVARIABLE = “tomorrowName”>

 

The period between “myComponents” and “dateExample” is the same as “/”. In fact, you can use a slash instead, but I think most developers will use dots since they look more Java-ish.

So now you have a basic mastery of ColdFusion components. There are actually many more CFC features that are worth exploring, such as component packaging, inheritance, polymorphism, security, and custom persistance to name a few. I suggest that you check out some of the articles at the CFMX Application Development Center for more information, or the recently launched CFCZone for a growing collection of free open source CFCs.

Next, we’ll take everything we’ve learned up to this point and use it to introduce Web services. We’ll check out how you use ColdFusion components to build Web services, and how we can utilize Web services hosted by other systems in our own applications.

Comments