Global Configuration in Model-Glue: Using Your Own Config CFC
In a post I did a while back, I shared what I had learned about using Model-Glue's built in "SimpleConfig" cfc. Well, I just got a question from a fellow developer who was trying to leverage the information in that post to implement his own home grown configuration settings cfc, and what I had shared there didn't quite apply. I tried to email him back, but it was returned as undeliverable, so I thought I'd just post my response to him here for the sake of anybody else who is endeavoring to do the same thing.
His email:
Hi Doug,
I love your post (http://www.dougboude.com/blog/1/2007/06/Global-Configuration-Settings-in-ModelGlueUnity.cfm)
I was actually finding it hard to implement though. I basically have written a settings.cfc with a medium sized struct containing all my site settings. I have functions like getProperty([name]) which returns the required value.
Now, I am just getting into MG and i am finding it hard to simply put this in the global scope available to my Models, Views and Controllers.
I read your article but am still confused.
Can you help us out with this?
Thanks
My response to...we'll call him Johnny:
Sure can, Johnny! Since you have created your OWN settings CFC, which I have also done in other MG apps, here's what I completely recommend doing: use Coldspring.
What we're going to do is let Coldspring handle the "injection" of our settings object into those other objects that need it. Let's say for an example that we have a USER.cfc in our model directory that will need the DSN in order to perform a query. The DSN value is located in our SETTINGS.cfc. So, in the Coldspring.XML file we're going to have to do two things:
1. tell Coldspring about our settings.cfc
2. tell Coldspring about our user.cfc, at the same time telling it to put 'settings' inside of 'user'
Now, two questions become immediately apparent:
1. What is the proper syntax for telling Coldspring the above two items?
2. What do I have to do special inside of my USER.cfc in order to help Coldspring do what I told it to do?
Here's what those two methods should look like:
By us including those two methods (notice the correlation between the names of the methods...get[bean id here] and set[bean id here] to the ID value we chose in the Coldspring.XML snippet), Coldspring will inject an instance of your settings object. Then, within any method inside of the User.cfc, we can access our settings
with "GetSettings().getProperty([name])".
For clarification, here is a sample USER.cfc to illustrate what I mean:
That's it!
For every CFC that requires an instance of your Settings.cfc, just make sure that you define it in Coldspring.XML AND include the Set and Get methods named appropriately inside of the recipient CFC.
Hope this helps, Johnny, and let me know if you have any more questions; I'm always glad to share what I've learned.
Doug :0)
His email:
Hi Doug,
I love your post (http://www.dougboude.com/blog/1/2007/06/Global-Configuration-Settings-in-ModelGlueUnity.cfm)
I was actually finding it hard to implement though. I basically have written a settings.cfc with a medium sized struct containing all my site settings. I have functions like getProperty([name]) which returns the required value.
Now, I am just getting into MG and i am finding it hard to simply put this in the global scope available to my Models, Views and Controllers.
I read your article but am still confused.
Can you help us out with this?
Thanks
My response to...we'll call him Johnny:
Sure can, Johnny! Since you have created your OWN settings CFC, which I have also done in other MG apps, here's what I completely recommend doing: use Coldspring.
What we're going to do is let Coldspring handle the "injection" of our settings object into those other objects that need it. Let's say for an example that we have a USER.cfc in our model directory that will need the DSN in order to perform a query. The DSN value is located in our SETTINGS.cfc. So, in the Coldspring.XML file we're going to have to do two things:
1. tell Coldspring about our settings.cfc
2. tell Coldspring about our user.cfc, at the same time telling it to put 'settings' inside of 'user'
Now, two questions become immediately apparent:
1. What is the proper syntax for telling Coldspring the above two items?
2. What do I have to do special inside of my USER.cfc in order to help Coldspring do what I told it to do?
Telling Coldspring About Your Objects
Look at the Coldspring.XML snippet below. It is accomplishing the first two items we said we needed to tell Coldspring about (our settings cfc and our user cfc):<bean id="Settings" class="model.settings" />
<bean id="User" class="model.user" >
<property name="Settings">
<ref bean="Settings"/>
</property>
</bean>
<bean id="User" class="model.user" >
<property name="Settings">
<ref bean="Settings"/>
</property>
</bean>
Accommodating Coldspring Within Our CFCs
Now, what do we do special inside our User.CFC in order to help Coldspring accomplish what we told it to do? We create two additional methods, one called SetSettings and one called GetSettings.Here's what those two methods should look like:
<CFFUNCTION name="SetSettings" access="public" returntype="void" output="false">
<CFARGUMENT name="Settings" type="model.settings" required="true" />
<CFSET variables._Settings = arguments.Settings />
</CFFUNCTION>
<CFFUNCTION name="GetSettings" access="public"
returntype="model.settings" output="false">
<CFRETURN variables._Settings />
</CFFUNCTION>
<CFARGUMENT name="Settings" type="model.settings" required="true" />
<CFSET variables._Settings = arguments.Settings />
</CFFUNCTION>
<CFFUNCTION name="GetSettings" access="public"
returntype="model.settings" output="false">
<CFRETURN variables._Settings />
</CFFUNCTION>
By us including those two methods (notice the correlation between the names of the methods...get[bean id here] and set[bean id here] to the ID value we chose in the Coldspring.XML snippet), Coldspring will inject an instance of your settings object. Then, within any method inside of the User.cfc, we can access our settings
with "GetSettings().getProperty([name])".
For clarification, here is a sample USER.cfc to illustrate what I mean:
<CFCOMPONENT DISPLAYNAME="user object" >
<CFFUNCTION name="SetSettings" access="public" returntype="void" output="false">
<CFARGUMENT name="Settings" type="model.settings" required="true" />
<CFSET variables._Settings = arguments.Settings />
</CFFUNCTION>
<CFFUNCTION name="GetSettings" access="public" returntype="model.settings" output="false">
<CFRETURN variables._Settings />
</CFFUNCTION>
<CFFUNCTION NAME="login" ACCESS="public" RETURNTYPE="string">
<CFARGUMENT NAME="username" TYPE="string" REQUIRED="yes" />
<CFARGUMENT NAME="password" TYPE="string" REQUIRED="yes" />
<cfset var retval = "" />
<CFQUERY NAME="LogMeIn" datasource="#GetSettings().getProperty("DSN")#">
select * from users where
username = <cfqueryparam value="#arguments.username#">
AND
password = <cfqueryparam value="#arguments.password#">
</CFQUERY>
<cfif LogMeIn.recordcount eq 1>
<cfset retval = "success" />
<cfelse>
<cfset retval = "failure" />
</cfif>
<CFRETURN retval />
</CFFUNCTION>
</CFCOMPONENT>
<CFFUNCTION name="SetSettings" access="public" returntype="void" output="false">
<CFARGUMENT name="Settings" type="model.settings" required="true" />
<CFSET variables._Settings = arguments.Settings />
</CFFUNCTION>
<CFFUNCTION name="GetSettings" access="public" returntype="model.settings" output="false">
<CFRETURN variables._Settings />
</CFFUNCTION>
<CFFUNCTION NAME="login" ACCESS="public" RETURNTYPE="string">
<CFARGUMENT NAME="username" TYPE="string" REQUIRED="yes" />
<CFARGUMENT NAME="password" TYPE="string" REQUIRED="yes" />
<cfset var retval = "" />
<CFQUERY NAME="LogMeIn" datasource="#GetSettings().getProperty("DSN")#">
select * from users where
username = <cfqueryparam value="#arguments.username#">
AND
password = <cfqueryparam value="#arguments.password#">
</CFQUERY>
<cfif LogMeIn.recordcount eq 1>
<cfset retval = "success" />
<cfelse>
<cfset retval = "failure" />
</cfif>
<CFRETURN retval />
</CFFUNCTION>
</CFCOMPONENT>
This sample assumes that your homegrown settings cfc uses a method called 'getProperty' to retrieve values
That's it!
For every CFC that requires an instance of your Settings.cfc, just make sure that you define it in Coldspring.XML AND include the Set and Get methods named appropriately inside of the recipient CFC.
A SPECIAL CASE
IF you are trying to inject your Settings.cfc into a Modelglue CONTROLLER cfc, then there's one less step involved for you. The controller CFC still needs the GET and SET methods present inside itself, BUT you do NOT need to define the controller bean within Coldspring.XML. Allow me to repeat myself: As long as the Settings bean is defined in Coldspring.XML, all you have to do to have that injected into a CONTROLLER cfc is to create a SET and GET method identical to the examples above within the recipient controller cfc.In Closing
I'll close by saying that there are other ways, using Coldspring, to get a Settings object into other objects, but in my opinion those approaches are no better or worse than the one I described above, so let's just keep it simple and stick to this one.Hope this helps, Johnny, and let me know if you have any more questions; I'm always glad to share what I've learned.
Doug :0)
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.