Using Constants in ColdFusion Components

In code we often need to make use of constant values. It is good practice to use named constants rather than literal values, but what is a good technique for managing constants in an object oriented ColdFusion application?

One technique is to break your constants down into separate files that relate to their particular area. For example you might have a set of files:

/constants/global.cfm

/constants/products.cfm

/constants/users.cfm

Etc.

Inside each file you would define the constants for that area. For example, for the products.cfm file you may write:

<cfscript>

// Define the product constant namespace.
structGet("constant.product");

// Product status constants.
constant.product.STATUS_ACTIVE = 1;
constant.product.STATUS_INACTIVE = 2;
constant.product.STATUS_ARCHIVED = 3;

// Other product related constants here, etc.
constant.product.IS_FEATURE = 1;
constant.product.IS_SPECIAL = 2;

</cfscript>

Here you can use the structGet() function to create a "namespace" for your constants. This means that if you have identical constant names in different areas then they will not overlap.

Then, wherever you need to use the constants you can include these directly into your components:

<cfcomponent output="false">

   <cfinclude template="/constants/global.cfm">
   <cfinclude template="/constants/products.cfm">
   
   <!--- Functions here... --->

</cfcomponent>

This would cause the constants to all be loaded into the component's variables scope. You could them use them in a function as follows:

<cffunction name="isActive" output="false">
   <cfreturn getStatusId() eq constant.product.STATUS_ACTIVE>
</cffunction>

This does not appear to be a particularly object oriented technique but seems a little nicer that some of the alternatives, such as referencing an external scope (eg constants stored in the application scope) or passing/injecting a "Constants" object into other objects that require it.

What do you think? How do you handle constants in ColdFusion components?

Comments
9 May 2008 08:23AM
"How do you handle constants in ColdFusion components"
I have ColdSpring inject them, normaly from a Map in the ColdSpring XML though if their more complicated or user-dependant I'll do a fill blown 'EnvironmentService' that can be injected into all the components.
9 May 2008 12:04PM
Like Tom I use ColdSpring for this. I have a MapFactoryBean that is declared with all of the constants I need and then I have that injected into my CFCs. It's very clean and simple.
9 May 2008 01:36PM
Thanks Tom, Sean

I originally started off with a ColdSpring bean of constants, but when working on a ColdSpring / Transfer application I could not see a simple way to get my constants into Transfer Decorators, so I switched to cfm's.

However, just now found Brian Kotek's Transfer bean injector:
http://www.briankotek.com/blog/index.cfm/2008/1/14...

Thanks Brian!
9 May 2008 04:59PM
For reference, the MapFactoryBean is a component available in the CVS version of ColdSpring. Using this component you can create a bean such as:

<bean id="appConstants" class="coldspring.beans.factory.config.MapFactoryBean">
   <property name="sourceMap">
      <map>
         <entry key="product">
            <map>
               <entry key="STATUS_ACTIVE"><value>1</value></entry>
               <entry key="STATUS_INACTIVE"><value>2</value></entry>
               <entry key="STATUS_ARCHIVED"><value>3</value></entry>
               <entry key="user">
                  <map>
                     <entry key="STATUS_ARCHIVED"><value>1</value></entry>
                     <entry key="ADMIN_USER_ID"><value>1</value></entry>
                  </map>
               </entry>
            </map>
         </entry>
         <entry key="user">
            <map>
               <entry key="STATUS_ARCHIVED"><value>1</value></entry>
               <entry key="ADMIN_USER_ID"><value>1</value></entry>
            </map>
         </entry>
      </map>
   </property>
</bean>

Then using code such as:

<cfset constants = factory.getBean("appConstants")>

You get a nested set of structures that may be used as constants.

I also noticed that you cannot use a bean with id="constants". This causes a bean creation exception.
Add a comment
(will not be published)
(include http://)