ColdBox and OO, Really?

Starting at the beginning I wanted push my knowledge a little more and continue with my venture into OO. Although I can recall most of the theory from my JAVA days at University, applying some of the concepts within ColdFusion does not seem to fit and in some instances proven difficult to get my head around.

I have come to the conclusion if I want to try and apply OO concepts to ColdFusion I must not think of OO and JAVA but an adaptation of the principles of OO reborn for ColdFusion (where did that come from? lol). Anyway with that said I had an eureka moment last night, ok, it came with help from the guys on the messages boards @ houseoffusion. I posted some code not fully understand the direction I needed to go in. Using ColdBox and ColdSpring I wanted to create a 'simple' example of a login which would help me better understand how to use objects containing 'getters' and 'setters'.

As it turns out its super easy in ColdBox, so easy infact I was looking for the hidden catch! Below is what I ended up with. It shows a function that is run in my handler to process a form post from a login page.

[More]

ColdBox and ColdSpring Hard Lessons

It has been a few weeks now of me getting to grips with ColdBox and ColdSpring. My question has been mainly on why I would use ColdSpring at all. Some will be glad to hear I am starting to see how ColdSpring helps with managing CFC's. I also just wanted to recommend Jason Dean's Blog. He has provided a very insightful step by step guide to using ColdBox and ColdSpring which has really helped me. I got it late, I know but if for some strange reason you have not read his series, do it, do it now!

The Hard Lesson

When I was creating my object i.e "userservice().init()" CF was throwing an error. It seemed ColdSpring knew nothing about my injected properties to the CFC, can you spot why? Well either ColdSpring or ColdBox (not sure which) handles the .init() it calls the constructor and passes in the correct argument without my help. So because I was trying to call the init() myself it was not being managed by ColdSpring. Simple, but it held me up for a long time. Because I always try and follow the best practice it never occurred to me this could be the culprit. A quick post on the ColdSpring user group and Brian Kotek quickly put me on the right track, thanks again.

[More]

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.

 

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