Check out our Angular Book Series.

Inversion of Control Design Pattern

I just found Charlie Arehart's new blog. In the comments of his second post Charlie and Ray start discussing the joys and pain of having two config files vs one config file.

One person I worked with turned me onto something he called Inversion of Control, which is a design pattern. I have found it is a rather elegant solution to the config file issue. This is how he set it up:

First create a CFC for your domain name (Jeffryhouser.cfc):


<cfcomponent output="false">
<cfscript>
this.dsn = "MyDsn"
this.otherconfig="OtherConfig"
</cfscript>
</cfcomponent>

This CFC is our master config file, and contains all the config information for our live server. It is named after the live server .

Now we need to set up a config file for the development machine; aka localhost.cfc:


<cfcomponent output="false" extends="jeffryhouser ">
<cfscript>
this.otherconfig="MyLocalCustomConfig"
</cfscript>
</cfcomponent>

This config file extends all the values from the first, and therefore inherits the all the this.x instance variables. We just overwrite our custom config options for the localhost. Most commonly, it is directory paths that change.

In this application we were loading the config values into the request scope. on the OnRequestStart event we added this code:


<cfset var ioc = REReplace(cgi.HTTP_HOST,"[[:punct:]].*$","")>
<cfset structAppend(request,createObject("component","ioc.#ioc#"))>

The first line gets the domain name. The second line instanstiates the component and copies all the instance variables into the request scope. I thought it was a pretty sweet approach and it has worked out pretty well. If Ray was so inclined to change it, I see no reason why Blog.cfc couldn't use a similar approach.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
All Content Copyright 2005, 2006, 2007, 2008, 2009 Jeffry Houser. May not be reused without permission
BlogCFC was created by Raymond Camden. This blog is running version 5.9.2.002.