Contact Doug!
Learn About Doug!
View Doug Boude's online resume
updated 11/18/2009

View Doug Boude's profile on LinkedIn
Link to me!

Follow Doug Boude on Twitter
Follow me!

Be Doug's friend on Facebook
Befriend me!
(I promise not to follow you home)
OO Lexicon
Chat with Doug!
Recent Entries
You may also be interested in...
Florida web site design



Czech your Page Rank!
Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service
Surf's Up!
Visit Egosurf.org and massage YOUR web ego!
My Score: 9,001
Doug's Books

Read (and recommend)

  • Men are from Mars, Women are from Venus
  • The Wisdom of Crowds: Why the Many Are Smarter Than the Few and How Collective Wisdom Shapes Business, Economies, Societies and Nations
  • Blink: The Power of Thinking Without Thinking
  • Head First Design Patterns
  • Transact-SQL Programming
  • What's So Amazing About Grace?
  • Just So Stories (Rudyard Kipling collection)

Reading

  • Prayer: Does it Make Any Difference?
  • Data Mining (Practical Machine Learning Tools and Techniques)
<< May, 2009 >>
SMTWTFS
12
3456789
10111213141516
17181920212223
24252627282930
31
Search Blog

Recent Comments
Categories
Archives
Photo Albums
Funnies (5)
Family (3)
RSS

Powered by
BlogCFM v1.11

10 December 2008
Using Database-Driven Configuration Settings in Coldbox
My approach

The Scenario: My Coldbox/Coldspring/Transfer application has settings that I want to make maintainable via a user interface. Now, the Coldbox.xml.cfm file does make provision for me to add as many custom settings as I want to my app, but I don't want to require the end user to know how to edit this xml file (nor would I trust them to). I came up with a solution to this challenge that I'm really happy with, so I thought I'd share some snippets in case it proves useful to others.

Step 1 is to ensure that you have a table designed to hold these configuration settings. My database has a table named "configuration", with the fields ID, settingName, and settingValue.

CREATE TABLE [dbo].[configuration](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [settingName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 [settingValue] [varchar](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [configuration_PK_UC1] PRIMARY KEY CLUSTERED
(
 [id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Of course, make sure this table is registered in your Transfer.xml.cfm file:

<package name="configuration">
 <object name="configuration" table="configuration" >
  <id name="id" type="numeric" generate="false"/>
  <property name="settingName" type="string" column="settingName" nullable="false"/>
  <property name="settingValue" type="string" column="settingValue" nullable="false"/>
 </object>
</package>

Next, I had to find the equivalent of the "onApplicationStart" within my Coldbox app because it is on application start that I wish to read in and load my settings. In Coldbox, this is an interception point named "afterAspectsLoad", which runs during application startup just after all plugins are created (important in my case especially since I'm relying on the "ioc" plugin to get Transfer for me). In order to leverage this interception point,  you'll need to create an interceptor and drop it in to your "interceptors" directory. Here is the interceptor I created that loads up my app's settings:

<cfcomponent name="customConfig"
    hint="I load settings stored in the database"
    output="false"
    extends="coldbox.system.interceptor">
   
 <cffunction name="Configure" access="public" returntype="void" hint="" output="false" >
  <cfset instance.ioc = getPlugin("ioc") />
 </cffunction>
 
 <cffunction name="afterAspectsLoad" access="public" returntype="void" output="false" hint="I load in any application settings from the database">
  <cfargument name="event" required="true" type="coldbox.system.beans.requestContext" />
  <cfset var qryConfigSettings = instance.ioc.getBean("transfer").list("configuration.configuration") />
  <cfset var tmpVal = "" />
  <cfloop query="qryConfigSettings">
   <cfif isJSON(settingValue)>
    <cfset tmpVal = deserializeJSON(settingValue) />
   <cfelse>
    <cfset tmpVal = settingValue />
   </cfif>
   <cfset setSetting(settingName,tmpVal) />
  </cfloop>
 </cffunction>
</cfcomponent>

As you may have noticed, I'm allowing my setting values to be JSON strings if needed, just in case I want to pass in an array or structure of values for a particular setting.

 

 

The last item of business is to let Coldbox know that our interceptor exists. We do this by registering it in the <Interceptors> section of Coldbox.xml.cfm, like so:

<Interceptor class="myapp.interceptors.customConfig" />

That's it! re-initialize your application (&fwreinit=1) and all of your settings will be available anywhere within your app.

 




Posted by dougboude at 3:54 PM | PRINT THIS POST! |Link | 0 comments
Subscription Options

You are not logged in, so your subscription status for this entry is unknown. You can login or register here.

No comments found.

Name:   Required
Email:   Required your email address will not be publicly displayed.

Want to receive notifications when new comments are added? Login/Register for an account.

Time to take the Turing Test!!!

0 plus 8 equals
Type in the answer to the question you see above:

Your comment:

Sorry, no HTML allowed!