ColdFusion

June 5th, 2012

Introduction to ColdFusion Components – CFC

More articles by »
Written by: Nouman Naveed
Tags:
adobe coldfusion component

In this article I am going to discuss about Adobe ColdFusion feature called ColdFusion Component which sometime called CFC in short form. ColdFusion Components allows you to group ColdFusion statements into user-defined function that perform logical unit of work. Multiple related functions can be stored in single CFC file to provide a higher degree of encapsulation and abstraction.

CFCs allows you to make your application more robust and maintainable by representing complex business logic as a series of highly abstracted and structured code libraries while allowing you to apply object based and object oriented development methodology. Commonly CFCs include non-UI logic for processing SQL commands or performing any transactions. CFCs file consists of Methods and Properties. You can assume CFC as a Java class and structure of CFC is created in similar fashion. There are two types of ColdFusion Components – Static and Instanced based components. Static CFC components are the simplest form and they are composed of one or more methods and also dont have any data structure bound to them. On the other hand Instance based CFC contain both methods and properties. Instance based CFCs which include properties should be created as persistent instance to use them and usually created in session and application scope variables.

Benefits of ColdFusion Components

There are various benefits when you use CFC in your ColdFusion application. The first advantage you get is performance, because it is noticed that CFC code executes more faster compared to any other logic written in CFM pages loosely. Second benefit you get is that it allows you to encapsulate your code into easily understood building blocks. CFCs can be called without including the code of the page and you can use object oriented features like inheritance and data hiding. Furthermore, if you have any Flex applications you can make it to call ColdFusion components through remoting  using AMF technology. It cab be called as a webservice by other clients, including Flash, .Net, PHP, and other platforms. Anytime if you want to understand the structure of CFC there is no need to have source code with you and you understand it by using a feature called self documenting through introspection. CFCs can persist between pages and request, this will allow you to reuse the same instance and invoke the component methods without reloading in memory for each method call. In addition it gives you a facility of securing it based on roles.

CFC Syntax

<cfcomponent>
<cffunction name=”function_name1″>
<cfargument argument_name” />
[CFML code]
<cfreturn expression_to_return>
</cffunction>
<cffunction name=”function_name2″>
<cfargument name=”argument_name” />
[CFML code]
<cfreturn expression_to_return>
</cffunction>
</cfcomponent>


Creating Component Properties

Component properties are declared immediately after the <cfcomponent> tag using this scope. These variables are global to the component and ofcourse accessible to all functions defined within the component.

Creating Functions in ColdFusion Components

As discussed previously, you can create multiple functions inside single ColdFusion Component file (sometimes referred to as methods). Functions are series of ColdFusion statements which are grouped into a logical unit of work. All functions within CFC need to be declared with <cffunction> tag. <cffunction> tag supports features like Data type validation for return data, Access scoping, Roles based security, control of white space & Protected variable scoping. There are two required attributes of <cffunction> tag in ColdFusion CFC, which are name & returntype.

Creating Function Parameters

To inject data inside the CFC function you need to use <cfargument> tag and define the name & type of argument. This tag also has optional attribute called “required” if set to true, then during invocation the parameter value must be passed. And to return value back to function caller you need to use <cfreturn> tag. The purpose of <cfreturn> tag is to return process logic result to caller of function. Here is an example which shows structure of sample CFC with methods and arguments defined.

<cfcomponent>
<cfset this.datasource = “acfd800-walk”>
<cffunction name=”get” returntype=”query”>
<cfargument type=”numeric” name=”categoryid” required=”false” default=”-1”/>
<cfset var q = “”>
<cfquery name=”q” datasource=”#this.datasource#”>
select *from category
<cfif arguments.categoryid ge 0 >
where categoryid =
<cfqueryparam cfsqltype=”cf_sql_numeric” value=”#arguments.categoryid#”>
</cfif>
</cfquery>
<cfreturn q>
</cffunction>
</cfcomponent>


About the Author

Nouman Naveed
My name is Mohammed Nouman Naveed. I work and play with Flash Platform technologies and has experience of 11 years. Consulting and Training is part of my job. I am Adobe Certified Instructor, Adobe User Group Manager, Adobe Certified Expert and Adobe Community Champion.




 
 

 
CQ5
adobe aem56 dam logo

Setting Images in DAM

All images are loaded into the DAM (Digital Assets Management). To upload an image, you’ll need to use your Activator login. From the tree, click +Digital Assets Find the way to the tree to suitable folder (AAFP, images, ANN,...
by Nouman Naveed
0

 
 
 

Apache Flex 4.10.0 Available

Here comes Apache Flex 4.10. This project, the all-volunteer group of developers, and stakeholders in the development framework, today announced the release of Apache Flex 4.10.0. This release makes some important improvements ...
by Nouman Naveed
0

 
 
CQ5
aem 5.6.1 features pic

Many New Features in AEM 5.6.1

AEM 5.6.1 is a easy to use, unified experience management collection for marketers in the digital sphere to use in creating, managing, and delivering unique online experiences that are brand-entered and increase demand across d...
by Nouman Naveed
0

 

 
CQ5
Adobe CQ5 WEM, SOCO, Tagging, DAM

What’s New in CQ 5.6

1: It’s called AEM (Adobe Experience Manager) now Though I guess not that essential, it’s going to be exciting if the name is going to attach – there were other occasions where Adobe tried to rename this result and at the...
by Nouman Naveed
0

 
 
CQ5
content management for web and mobile

Content Management for Web & Mobile

Adobe CQ is a WCM and gives a better browser-based user interface. It is best suitable for business and IT for creating interactive, multimedia rich, multichannel consumer understanding to make an engaging digital existence, ma...
by Nouman Naveed
0

 




0 Comments


Be the first to comment!


Leave a Reply

Your email address will not be published. Required fields are marked *