ColdBox using ColdSpring

The more I work in ColdBox the more I want to learn, so pardon my ramblings but yesterday ColdBox and ColdSpring took my fancy. I have never worked with ColdSpring nor any other AOP for CFC's.

So ColdSpring, what's all that about then? To be honest I am still not sure why I would use it. I started by playing around, I wanted to inject my ColdBox DNS setting into my common.cfc (database gateway).

   view plainprintabout
 <beans default-autowire="byName">
 <bean id="coldboxFactory" class="coldbox.system.extras.ColdboxFactory" />
 <bean id="ConfigBean" factory-bean="ColdboxFactory" factory-method="getConfigBean" />
 <bean id="dsnBean" factory-bean="ColdboxFactory" factory-method="getDatasource">
     <constructor-arg name="alias">
     <value>DBDetails</value>
     </constructor-arg>
 </bean>    
     <bean id="userService" class="model.UserService" />
10      
11      <bean id="userGateway" class="model.common" >
12       <property name="dsnBean">
13   <ref bean="dsnBean" />
14   </property>
15   </bean>
16  
17  </beans>

I then created a UserService.cfc. The idea I think behind this is that I access the userService.cfc to get to common.cfc (gateway) and common.cfc gets the DNS setting injected.

   view plainprintabout
 <cfcomponent name="User Service">
 
 <cffunction name="init" access="public" returntype="any" hint="Constructor.">
  <cfreturn this />
 </cffunction>
 
 <cffunction name="getUserGateway" access="public" returntype="any" output="false" hint="Return the UserGateway.">
  <cfreturn variables.instance['userGateway'] />
 </cffunction>
10  
11  <cffunction name="setUserGateway" access="public" returntype="void" output="false" hint="Set the UserGateway.">
12   <cfargument name="userGateway" type="any" required="true" hint="UserGateway" />
13   <cfset variables.instance['userGateway'] = arguments.userGateway />
14  </cffunction>
15  </cfcomponent>
   view plainprintabout
 common.cfc
 <cffunction name="setdsnBean" access="public" returntype="void" output="false" hint="Set the dsnBean.">
  <cfargument name="dsnBean" type="any" required="true" hint="dsnBean" />
  <cfset variables.instance['dsnBean'] = arguments.dsnBean />
 </cffunction>

Why is this any better? I could have achieved this all in the init() method without ColdSpring, or I could have extended my CFC, or I could have even created a config CFC and injected that into the common.cfc.

Sorry, If these questions are just ignorant.

Related Blog Entries

TweetBacks
Comments
Henry Ho's Gravatar Why would you want to use a dsnBean at the first place?

Wouldn't it be better to inject the dsn (string) directly into UserGateway?

Read this: http://www.coldspringframework.org/coldspring/exam...
# Posted By Henry Ho | 24/07/09 19:41
Glyn Jackson's Gravatar I see what you mean in the coldspring.xml it's creating a bean then injecting that into common.cfc, this was taken from the the ColdBox examples. I am assuming there is a reason for this.
# Posted By Glyn Jackson | 25/07/09 16:25
Henry Ho's Gravatar I would not couple my model to Coldbox through use of dsnBean. Just inject the dsn into your DAO/Gateway through constructor-arg is generally better.
# Posted By Henry Ho | 25/07/09 16:32
Peter Bell's Gravatar Why use ColdSpring? Mainly to decouple your CFC's for testing (it also provides a single place to put a lot of config settings, but that is a secondary benefit).

Let's say you have a UserService that requires a UserValidator object. You could just createObject() within UserService.init() or call some kind of factory (application.beanFactory.getBean("UserValidator").

In the first case, as the complexity of the config settings for UserValidator grows, you need to maintain that in your UserService code (all the properties you need to pass to the init() constructor, etc).

In the second case, you've put all the knowledge about creating beans into one factory to make it easy to maintain, but you're still calling the application scope, so there is no easy way to mock/stub out the UserValidator implementation in your unit tests for the UserService.

If all this sounds like double dutch, you may not need a DI engine like ColdSpring or LightWire right now. Once you notice that you're having problems writing unit tests for your objects or with the complexity of the initialization of your cfc's all over the place, time to come back and have another look!

Peter
# Posted By Peter Bell | 29/07/09 18:53
 

About Me

Glyn Jackson, 27 years old, MD and senior developer of a development firm based in Staffordshire called Newebia Ltd. Academic background in BSc Information System & Internet Commerce. Online marketing expert (EE Ranked) and .NET developer. Has been developing with ColdFusion for 5 years and loves it. "I am not a veteran in ColdFusion but I do work on challenging projects which help me learn more about ColdFusion and if I can contribute to the community in anyway then, it's all good!"

Recommends

  • ColdFusion