<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cranky Bit &#187; ColdSpring</title>
	<atom:link href="http://blog.crankybit.com/tag/coldspring/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.crankybit.com</link>
	<description>Take a byte out of tech!</description>
	<lastBuildDate>Wed, 09 Mar 2011 17:23:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Importance of Understanding Factories, Part 1: ColdSpring Misunderstandings</title>
		<link>http://blog.crankybit.com/factories-1/</link>
		<comments>http://blog.crankybit.com/factories-1/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 13:33:26 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[General Web Dev.]]></category>
		<category><![CDATA[ColdSpring]]></category>
		<category><![CDATA[factories]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://blog.crankybit.com/?p=326</guid>
		<description><![CDATA[As ColdFusion developers progress in the use of object oriented programming concepts with CFCs, our applications become more flexible and able to support additional interfaces such as Ajax or Flex through web services. We also potentially benefit just from overall code maintainability, extensibility, and reusability by using best practices and design patterns common in OOP. [...]]]></description>
			<content:encoded><![CDATA[<p>As ColdFusion developers progress in the use of object oriented programming concepts with CFCs, our applications become more flexible and able to support additional interfaces such as Ajax or Flex through web services. We also potentially benefit just from overall code maintainability, extensibility, and reusability by using best practices and design patterns common in <abbr title="object-oriented programming">OOP</abbr>. </p>
<p>The benefits are many, but there are certainly pitfalls along the way. Perhaps we're ready to begin using service objects. And we'd like to create and pass in our transient objects with ColdSpring as a factory for all objects.</p>
<p>Let's cover the groundwork of our example, and then consider a pitfall. Here's our transient object:</p>
<p><strong>Bean.cfc</strong></p>
<div class="igBar"><span id="lcfm-5"><a href="#" onclick="javascript:showCodeTxt('cfm-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-5">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfcomponent</span> <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Represents a bean."</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returntype=<span style="color: #009900;">"Bean"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"x"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"y"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"z"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"any"</span> <span style="color: #0000FF">required</span>=<span style="color: #009900;">"true"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setX<span style="color: #0000FF;">&#40;</span>arguments.x<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setY<span style="color: #0000FF;">&#40;</span>arguments.y<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> setZ<span style="color: #0000FF;">&#40;</span>arguments.z<span style="color: #0000FF;">&#41;</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------- Config -------------------------&gt;</span></span>&nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setConfig"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output=<span style="color: #009900;">"false"</span> returntype=<span style="color: #009900;">"void"</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Method to pass in configs."</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"Settings"</span> <span style="color: #0000FF;">type</span>=<span style="color: #009900;">"struct"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.config=arguments.settings/<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------ Setters -----------------------&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setX"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.x=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setY"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.y=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"setZ"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"newval"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.instance.<span style="color: #0000FF;">data</span>.z=arguments.newval /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #808080; font-style: italic; background-color:#FFFF99;">&lt;!------------------ Getters -----------------------&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getMemento"</span> <span style="color: #0000FF">access</span>=<span style="color: #009900;">"public"</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; returntype=<span style="color: #009900;">"struct"</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> variables.instance /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is an overly-simplified example of a bean. It just has 3 properties (X, Y, Z) and corresponding setter methods. It also has a typical <code>init()</code> method, a <code>setConfig()</code> method to pass in configuration, and a getter method so we can quickly dump the state of the object. It'll be enough for our samples.</p>
<p>Perhaps we might write a ColdSpring configuration like this:</p>
<div class="igBar"><span id="lxml-6"><a href="#" onclick="javascript:showCodeTxt('xml-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">XML:</span>
<div id="xml-6">
<div class="xml">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"BeanService"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.BeanService"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Bean"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">"Bean"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">"Bean"</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"com.Bean"</span> <span style="color: #000066;">singleton</span>=<span style="color: #ff0000;">"false"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"Config"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;entry</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">"dsn"</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>MyDSN<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/entry<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/map<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/property<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"x"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>One<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"y"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Two<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;constructor</span>-arg <span style="color: #000066;">name</span>=<span style="color: #ff0000;">"z"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;value<span style="font-weight: bold; color: black;">&gt;</span></span></span>Three<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/value<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/constructor</span>-arg<span style="font-weight: bold; color: black;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/bean<span style="font-weight: bold; color: black;">&gt;</span></span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This seems like it should work, eh? We're instantiating a service object, instantiating and configuring a bean, and passing the bean into the service object. Here's a really simple example of the bean service object:</p>
<p><strong>BeanService.cfc</strong></p>
<div class="igBar"><span id="lcfm-7"><a href="#" onclick="javascript:showCodeTxt('cfm-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-7">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cfcomponent</span> <span style="color: #0000FF">hint</span>=<span style="color: #009900;">"Handles beans."</span> output=<span style="color: #009900;">"false"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"init"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfargument</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"Bean"</span> /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfset</span> variables.Bean=Arguments.Bean /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> this /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"getBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cfreturn</span> variables.Bean /<span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;/cffunction&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"workWithBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> myBean=variables.Bean;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setX<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed X on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; myBean.setY<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed Y on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; return myBean;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cffunction&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cfcomponent&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This BeanService object has a constructor that receives the bean and stores it in its variables scope. It then has a the <code>getBean()</code> method so we can see the bean, and a <code>workWithBean()</code> method that simulates doing some work with the bean.</p>
<p><strong><em>And now, the pitfall: ColdSpring singletons and objects passed by reference.</em></strong></p>
<p>This code technically runs, and appears fine: The Bean instance that we pass into the service is <em>not</em> a singleton, so each BeanService we instantiate (<em>if</em> we were to instantiate multiple) would have a separate Bean instance. And <code>workWithBean()</code> appears to use a local var copy of Bean.</p>
<p>Wrong. </p>
<p>First, the <a target="_blank" href="http://www.coldspringframework.org/coldspring/examples/quickstart/index.cfm?page=singletons">Singleton vs. Transient Objects With ColdSpring</a> page explains that ColdSpring isn't ideal for generating transient components. Its overhead is more appropriate for less frequent creation of components that typically go in a shared scope, such as in the Application scope. Performance aside, though, the approach of injecting a transient bean into a singleton like BeanService does technically work.</p>
<p>So now the second point, how BeanService is working with Bean in our example <code>workWithBean()</code> method. The key misunderstanding is that CFCs like the Bean object are <em>passed by reference</em> when being assigned to variables. So when we do <code>var myBean=variables.Bean</code>, the local myBean variable is pointing to the same copy of Bean that <code>variables.Bean</code> points to. When it executes methods that change the bean's state, the one copy of Bean is being altered. Whoah, that wasn't an intended behavior. This could have very negative consequences, with Bean not being in the state you might expect it to be in.</p>
<p>So, how to fix it? Hmm. I don't want to use <code>CreateObject()</code> because that would become a dependency we'd have to manage, and we'd have to handle properly configuring the bean. This was the whole point of injecting the bean with ColdSpring in the first place.</p>
<p>Well, a quick solution could be to just use <code>Duplicate()</code> to make a copy of the injected bean before working on it! So, we could replace our <code>workWithBean()</code> method with this:</p>
<div class="igBar"><span id="lcfm-8"><a href="#" onclick="javascript:showCodeTxt('cfm-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CFM:</span>
<div id="cfm-8">
<div class="cfm">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #333333;"><span style="color: #990000;">&lt;cffunction</span> <span style="color: #0000FF;">name</span>=<span style="color: #009900;">"workWithBean"</span><span style="color: #990000;">&gt;</span></span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000099;"><span style="color: #990000;">&lt;cfscript&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> myBean=<span style="color: #0000FF;">Duplicate</span><span style="color: #0000FF;">&#40;</span>variables.Bean<span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; myBean.setX<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed X on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; myBean.setY<span style="color: #0000FF;">&#40;</span><span style="color: #009900;">"I changed Y on the bean!"</span><span style="color: #0000FF;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; return myBean;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #990000;">&lt;/cfscript&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #990000;">&lt;/cffunction&gt;</span> </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now, <code>variables.Bean</code> will always be the pristine, original bean injected by ColdSpring, and the local myBean variable will perform its work with a duplicate of that bean. Our code will now be working the way we intended; disaster has been averted.</p>
<p>Well, this <em>does</em> work. But it is neither graceful nor efficient, and it is <em>not</em> recommended. Consider Sean Corfield's blog post, <a href="http://corfield.org/blog/index.cfm/do/blog.entry/entry/duplicate_is_bad_for_your_objects_health" target="_blank">Duplicate() is Bad For Your (Object's) Health</a>. In an informal test, he clocked Duplicate() as being drastically slower&mdash;we're talking 50 to 1&mdash;than using <code>CreateObject()</code>. </p>
<p>Ugh. If we're back to using <code>CreateObject()</code>, what good is using ColdSpring for managing these component relationships, and what about dealing with configuration? <strong><em>Enter the world of factories.</em></strong> Yes, ColdSpring itself is a factory, but <em>it is a factory oriented around creating and configuring our singletons,</em> which can <em>include other factories</em> whose purpose is to properly create transient objects like Bean. </p>
<p>So instead of having ColdSpring inject Bean into BeanService, we can make a BeanFactory and inject <em>that</em> into BeanService, giving BeanService the ability to create Beans to work with any time it needs to. This may seem like a lot of effort, but we'll come to see that creating transient factories is easy and will clean up the mess of an architecture in this sample to something graceful and maintainable.</p>
<p>In this post, I described a few of the <em>mistakes</em> that can be made as we start to utilize ColdSpring. Hopefully these mistakes point to the importance of factories&mdash;not just ColdSpring, but our own transient factories as well. In the next blog post, we'll rework the sample to use a factory and review the benefits the factory provides and how it doesn't duplicate the purpose of ColdSpring. We'll review one more scenario that underscores the importance of factories: the dreaded race condition. And I'll provide source code that has executable demonstrations of these undesirable mistakes in action. <img src='http://blog.crankybit.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.crankybit.com/factories-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

