Welcome!

You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Jeremy Geelan, Maureen O'Gara, Nancy Y. Nee, Tad Anderson

Related Topics: ColdFusion

ColdFusion: Article

ColdFusion Components

A powerful tool for CF developers

I'm writing this article a few weeks before MAX takes place. I'll be one of the unfortunate few unable to join everyone for the festivities. I am keeping a close eye on the Blackstone development though, and can't wait to start writing about it in this column.

As a side note, I will be at the Powered by Detroit conference next April (mentioned in Simon's Community Column in last month's issue of CFDJ); more details on that to come (www.poweredbydetroit.org/).

This month, we're going to talk about ColdFusion Components, or CFCs as they are more commonly known. CFCs were one of the most significant features put into the ColdFusion MX release. If you haven't read last month's article on user defined functions, and are unfamiliar with them, you may want to read that before going further. User defined functions allow you to take snippets of functionality and put them into their own little package. CFCs take this concept one step further and allow you to combine data and functionality into its own little package.

The Elements of a ColdFusion Component
If you've done any development with ColdFusion, it is likely that you already know what a ".cfm" template is. You put ColdFusion code in a .cfm template and the Web server knows to send requests for that file to the ColdFusion server for processing. ColdFusion components are contained in their own file, with the extension ".cfc". The extension is how ColdFusion distinguishes the difference between ColdFusion templates (.cfm) and ColdFusion Components (.cfc).

There is more to a component than just creating a file with the extension .cfc. There are some tags involved, too. Everything in a component is wrapped in the cfcomponent tag. These are its attributes:

  • Extends: When you're dealing with inheritance, the "extends" attribute specifies the name of the component that this component will inherit its properties and methods from. This is an optional attribute.
  • Output: The output attribute specifies whether the component body is allowed to generate output. If you set this value to yes, then everything inside the component, but not in a method, is processed as if it were inside a cfoutput tag of a standard template (I'll tell you shortly about component code that's not in a method). Methods will inherit this property, but it can be overridden at the function level. If you set this to no, then the component code outside of methods is processed as if inside a cfsilent tag. That is to say, all output is suppressed (more information about cfsilent is at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-pc7.htm#wp1103549). If you omit this attribute, then the code is processed as standard CFML. It's rare and not recommended to output data from inside of a component, so I will set this attribute to no in the examples in this article.
  • DisplayName: ColdFusion Components are self-documenting. Attempting to browse directly to a CFC is intercepted by a component browser utility that displays the component documentation on the screen. The process of generating documentation in this manner is called introspection and the data displayed is called metadata. When you view the metadata of a compoment, the DisplayName attribute is listed next to the component's name. This is an optional attribute. It has no effect on functionality.
  • Hint: The hint attribute is another attribute that helps the CFC self-documenting feature. The text specified in the hint is displayed as part of the documentation. It's optional. It has no effect on functionality.
The interesting thing about the cfcomponent tag is that it is not necessarily needed in a cfc file. A cfc file is inherently a component, so we don't need to define it as such. In reality, the cfcomponent tag is almost always used, due to its attributes such as output and extends. If you really wanted to, you could use the cfcomponent tag in a cfm file to create a component.

Inside the cfcomponent tag you can define your component methods using the cffunction tag. Each component can have as many methods as you need. More information on cffunction can be found at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p43.htm#wp2852457. We also talked about functions in last month's column. These are the attributes to cffunction that are especially useful inside of CFCs:

  • Roles: The roles attribute is used to specify the roles that are allowed to access this function. This refers to the roles that are set when a user logs in using the ColdFusion cfloginuser tag. You can use this to control the users who can access individual component methods.
  • Access: The access attribute specifies the type of access a user must have to execute the function. The valid values for this are Private, Package, Public, and Remote. The default is Public, which means it can be executed by any local file. Private means only code inside the component can execute this function. Package means that only code inside the component package can execute it. A Package is all of the components in the same directory and subdirectories. Remote means it can be invoked remotely via Web services or Flash Remoting or (though rarely done) via URL GET or FORM POST operations.
  • Output: The output attribute on cffunction acts just like the output attribute on cfcomponent. If set to yes, then the body of the function will generate output. If set to no, then the body of the function will be treated as if it were inside a cfsilent block. In this article I'll set this to no, unless I'm trying to debug a function or if I'm creating a function whose sole purpose is to output data.
  • Displayname: Displayname is an optional attribute used only for display in the metadata of a component. Similar to the attribute of the cfcomponent tag, this attribute displays its value in parenthesis after the name of the function when viewing metadata and has no effect on performance.
  • Hint: The hint attribute is used to display comments or additional documentation in the component metadata. It is displayed under the function name as part of the metadata. It has no effect on functionality.
Other cffunction attributes, such as name and returntype still apply to functions inside a cfcomponent. The functions inside a component are often called methods, and the two terms can be used interchangeably: most developers will understand what you're talking about.

You may say: "I see that you mentioned something about code inside a component, but not inside a function. What can you tell me about that?" I'm glad you reminded me. The code in between cfcomponent tags, but not inside a cffunction block, is executed when you first create the component instance, but not when you make any subsequent calls on an existing instance. This is sometimes called "constructor" code. A constructor is an object-oriented programming (OOP)term that refers to a method that is executed when you create an object. A constructor is officially a method in OOP terminology, and since the CFC form of initialization is not a method, it is often called a pseudoconstructor.

Variables Inside a Component
There are two primary elements in a component: data and functionality. In the previous section we explored how to write the functionality. Here, you'll learn about the data. The data is stored in variables. You probably know all about variables in standard cfm templates. You can create them with cfset tags and they reside in different variable scopes, depending on the intended purpose of the variable. You can read a lot more about this in the first edition of this column (February '04).

Since you know all about variables, I'm going to jump right over the basics and go right into the scopes that exist inside a component:

  • This: This is a reference to everything public inside the component and can be thought of as the component's public scope. If you create a variable in the "this" scope, the variable can be accessed by anything that operates on the instance of the component. I don't usually use the "this" scope for variables for reasons you'll soon understand.
  • Variables: You'll recognize the variables scope from your work with cfm templates. In a component, the variables scope is used to hold private data. This data is sometimes called instance data.
  • Arguments: The arguments scope exists only inside a method, during the execution of a function. It contains all the arguments passed into the function (very similar to the "attributes" scope when creating custom tags).
  • Local function scope: The local function scope is an unnamed scope. All variables that you create with the "var" keyword using CFSET or CFSCRIPT exist in this scope.
  • Shared scopes: Inside a cfcomponent, you can access the shared scopes, such as application and session. Since the component is part of a request, request-based scopes such as URL, form, and request, are available to the component. Accessing these scopes is generally considered bad practice because it makes the code less reusable.
When developing components, I will use the "constructor" code to initialize variables in the components variables scope. I will create a number of methods to retrieve the private values (called getter methods because they only return a value) and methods to set the value of the private variables (called setter methods because they set a value).

Calling Components
There are a handful of different methods you can use to call components. I'm going to explain the one that I use most often. It's a two-step process, but is much more fun than country dancing. The first step is to create the component instance. The second step is to call various methods against the component. Component data will be persisted between component requests. Here is a sample component, entitled foo.cfc:


<cfcomponent>

 <cfset variables.bar = " corner">

 <cffunction name="Getbar" access="public" returntype="string">
  <cfreturn variables.bar>
 </cffunction>

 <cffunction name="Setbar" access="public" returntype="boolean">
  <cfargument name="bar" type="string" required="Yes">
  <cfset variables.bar = arguments.bar>
  <Cfreturn true>
 </cffunction>

</cfcomponent>

The foo component is very simple. It has one instance value, named bar. The bar variable is initialized to a string value "chunky".

To use the CFC you first need to create the instance of the component. You can use the cfobject tag or the createObject function to create the instance. Conceptually, both are similar. You can read about the createObject function at livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi45.htm#wp4569859. I'll explain the cfobject tag in more detail. It has two attributes in relation to components:

  • Component: The component attribute specifies the location of the component, including the name of the file without the cfc. If they are in the same directory you can just use the component name.
  • Name: The name of the return variable that will contain the component instance.
More information about using cfobject to invoke components is located at livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-pb7.htm#wp2651979. This is a snippet of code to invoke our foo component:

<cfobject component="Foo" name="objFoo">

To invoke a method on the component, we use the cfinvoke tag. These are its attributes:

  • Component: The component attribute is used to specify the name of component instance. To reference an instance that is already created, you must put it in pound signs. If you are calling a single method of a CFC without creating an instance, you can enter the location of the CFC.
  • Method: The method attribute is used to specify the name of the method you want to execute.
  • ReturnVariable: The ReturnVariable attribute specifies the name of the variable that is populated with the return value.
  • ArgumentCollection: The argument collection attribute is used to specify the name of a structure that contains all arguments that the function requires. It is optional. You can also list arguments using name value pairs that correspond to the arguments in the function, or by using cfinvokeargument tags nested inside the cfinvoke block.
After creating our instance, we can use the cfinvoke tag to check the value of the bar instance variable:


<cfinvoke component="#objFoo#" method="getBar" returnvariable="variables.Out"/>
<cfoutput>
 #variables.Out#
</cfoutput>

This code calls the GetBar method, putting the return variable in the out value and then outputting the value. You'll see that the Bar instance variable has not changed since we set it. We can change that using this line of code:

<cfinvoke component="#objFoo#" method="SetBar" bar="Main"/>

Rerun the previous segment after running the Set method and you'll see that the value has correctly changed.

Are CFCs Object-Oriented?
"So, Jeff," you say, "when I create a CFC it sounds like I'm creating an object. Isn't that object-oriented programming? I've heard that the use of CFCs is object-oriented. Isn't that true?" Again, I'm glad you asked. The answer is, quite indirectly, "sort of." There is no doubt that CFCs bring many object-oriented (OO) features into the CFML language. However, just because you are using CFCs does not mean you are programming in an OO way.

When we talk about object-oriented programming or traditional (procedural) programming, we're really talking about the approach taken to design an application. In ideal cases, this application design occurs before you sit down to write code. In CF5 and before, it was not as easy to implement an object-oriented design using ColdFusion. There is a group of custom tags called cfObjects that were used to apply OO design to ColdFusion. More info is available at http://cfobjects.sourceforge.net/. ColdFusion Components make the implementation of object-oriented designs much easier.

"So, then," you ask, "CFCs are object oriented, right?" They can be, yes. But, even if you are not using an object-oriented design you can still make use of CFCs. Instead of creating classes for an OO design, you can use CFCs to create Abstract Data Types (ADTs). In simple terms, an ADT is a user-defined data type. Just like classes in OO design, Abstract Data Types provide a way to encapsulate data and the functionality to operate on that data into one component. ADTs generally do not make use of OO specific concepts, such as inheritance or polymorphism.

Conclusion
ColdFusion Components are a powerful tool in the toolbox of any ColdFusion developer. They provide advanced encapsulation, so that you can more easily write reusable components and speed the development of future applications. They also open the door to the use of Web services and Flash Remoting. After reading this article, you should understand all the basics of creating CFCs. Next month, I'm going to show you how I like to use CFCs and I'll step you through the process of creating a reusable component.

More Stories By Jeffry Houser

Jeffry is a technical entrepreneur with over 10 years of making the web work for you. Lately Jeffry has been cooped up in his cave building the first in a line of easy to use interface components for Flex Developers at www.flextras.com . He has a Computer Science degree from the days before business met the Internet and owns DotComIt, an Adobe Solutions Partner specializing in Rich Internet Applications. Jeffry is an Adobe Community Expert and produces The Flex Show, a podcast that includes expert interviews and screencast tutorials. Jeffry is also co-manager of the Hartford CT Adobe User Group, author of three ColdFusion books and over 30 articles, and has spoken at various events all over the US. In his spare time he is a musician, old school adventure game aficionado, and recording engineer. He also owns a Wii. You can read his blog at www.jeffryhouser.com, check out his podcast at www.theflexshow.com or check out his company at www.dot-com-it.com.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
When growing capacity and power in the data center, the architectural trade-offs between server scale-up vs. scale-out continue to be debated. Both approaches are valid: scale-out adds multiple, smaller servers running in a distributed computing model, while scale-up adds fewer, more powerful servers that are capable of running larger workloads. It’s worth noting that there are additional, unique advantages that scale-up architectures offer. One big advantage is large memory and compute capacity...
Artificial intelligence, machine learning, neural networks. We’re in the midst of a wave of excitement around AI such as hasn’t been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. Will this time be different? Most likely. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Furthermore, the funding and research going into AI now comes from a wide range of com...
No hype cycles or predictions of zillions of things here. IoT is big. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, Associate Partner at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He discussed the evaluation of communication standards and IoT messaging protocols, data analytics considerations, edge-to-cloud tec...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
We build IoT infrastructure products - when you have to integrate different devices, different systems and cloud you have to build an application to do that but we eliminate the need to build an application. Our products can integrate any device, any system, any cloud regardless of protocol," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
The Internet giants are fully embracing AI. All the services they offer to their customers are aimed at drawing a map of the world with the data they get. The AIs from these companies are used to build disruptive approaches that cannot be used by established enterprises, which are threatened by these disruptions. However, most leaders underestimate the effect this will have on their businesses. In his session at 21st Cloud Expo, Rene Buest, Director Market Research & Technology Evangelism at Ara...
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
SYS-CON Events announced today that MobiDev, a client-oriented software development company, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MobiDev is a software company that develops and delivers turn-key mobile apps, websites, web services, and complex software systems for startups and enterprises. Since 2009 it has grown from a small group of passionate engineers and business...
SYS-CON Events announced today that GrapeUp, the leading provider of rapid product development at the speed of business, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market acr...
SYS-CON Events announced today that Ayehu will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara California. Ayehu provides IT Process Automation & Orchestration solutions for IT and Security professionals to identify and resolve critical incidents and enable rapid containment, eradication, and recovery from cyber security breaches. Ayehu provides customers greater control over IT infras...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
In his session at Cloud Expo, Alan Winters, an entertainment executive/TV producer turned serial entrepreneur, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to ma...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...
SYS-CON Events announced today that CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.