ColdSpring autowire byType plus ColdFusion 9 accessors means less code

ColdFusion 9, ColdSpring

During a great presentation by Raymond Camden on ColdSpring (slides) he showed off the cool autowire byType feature which got me thinking about using the property definitions in ColdFusion 9 to write less code. When default-autowire="byType" is set in ColdSpring it will inspect your CFC's and if the type of an argument is a defined bean it will inject it into the CFC.

Lets take a look at how this works:

<cffunction name="setShoppingCartService" access="public" returnType="void" output="false">
	<cfargument name="shoppingCartService" type="root.model.ShoppingCartService" required="true">
	<cfset variables.shoppingCartService = arguments.shoppingCartService>
</cffunction>

The key part is the type attribute of the cfargument tag.  If ColdSpring can match a defined bean of that type it will inject that CFC.  Very cool but there is a fair amount of code required to do this. 

Enter ColdFusion 9.  First lets set accessors to true:

component accessors="true" {

With accessors on each defined property will have a getX and setX function.  So by writing:

property name="shoppingCartService" type="root.model.ShoppingCartService";

Now, when ColdSpring does autowire coolness it will find the type and inject the CFC.

[UPDATE, 12/7/09 9.12PM EST] For reference here is what the ColdSpring config.xml file looks like:

<beans default-autowire="byType">
	
<bean id="ShoppingCartService" class="root.model.ShoppingCartService">
  <constructor-arg name="dsn">
    <value>${dsn}</value>
  </constructor-arg>
</bean>

</beans>

Also credit to Raymond Camden for the basis of these examples which where taken from the example code from his presentation. [END UPDATE]

Which is pretty cool stuff and another example of how ColdFusion 9 opens up new possibilities for coding ColdFusion applications.

In the chat session of the presentation Penny brought up the point that autowiring byType, or byName, means that the main ColdSpring config file does not contain information about where a CFC is used.  Personally I am fine with that but I think its a valid point and one for each development team and developer to work out.

Henry Ho said:
 
What will happen if I have two beans that are both from the same class?
 
posted 234 days ago
Add Comment Reply to: this comment OR this thread
 
samfarmer said:
 
@Henry That would work fine as long as the name of the property was different (which they would have to be to compile).
 
posted 234 days ago
Add Comment Reply to: this comment OR this thread
 
Mike Chandler said:
 
Pretty nifty CF9 feature with the accessors="true" attribute. I had begun working on a project in CF9 that made heavy use of that new feature only to have to circle back to a project on CF8... boy do I miss that! ;)
 
posted 234 days ago
Add Comment Reply to: this comment OR this thread
 
Kevin Penny said:
 
Yeah - we have a very shared model where some applications use only portions of the main classes, and others use the majority of them - so for us, having that granular control was essential - and thus making each application have their own configuration was necessary. It also allows us to expose only the necessary components and wire them up on an as needed basis - which can cut down on the initial load time dramatically, vs. having CS autowire mostly everything together for all apps -

One of those decisions I look back and am glad we chose to go configuration over 'autowire' in this case - but yes, each team will differ ;) Thanks Sam.
 
posted 234 days ago
Add Comment Reply to: this comment OR this thread
 
samfarmer said:
 
@Kevin. Good points. Sounds like you are doing some pretty complex and cool stuff.
 
posted 232 days ago
Add Comment Reply to: this comment OR this thread
 

Search

Twitter
You should follow me on Twitter here
About Me
I am a 34-year old Web Developer specializing in ColdFusion. I live and work in downtown Washington, DC with my wife and two daughters. Read more About Me

2007 CFeMmy Best Newcommer winner
As voted on by fellow CF Bloggers.