<?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>shaun smith &#187; dependency injection</title>
	<atom:link href="http://shaun.boyblack.co.za/blog/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://shaun.boyblack.co.za/blog</link>
	<description>Flash, Flex, Ruby - Cape Town, SA</description>
	<lastBuildDate>Wed, 08 Sep 2010 17:56:48 +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>AS3 DI Metadata Tags</title>
		<link>http://shaun.boyblack.co.za/blog/2010/09/08/as3-di-metadata-tags/</link>
		<comments>http://shaun.boyblack.co.za/blog/2010/09/08/as3-di-metadata-tags/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 17:56:48 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Banter]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[metadata]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=964</guid>
		<description><![CDATA[Spring Actionscript, Dawn, SmartyPants-IOC, Swiz, Spicelib (used by Parsley) and SwiftSuspenders (used by Robotlegs) perform Dependency Injection. Moreover they can all perform automatic DI by parsing class metadata. It would be grand if the common DI related metadata tags could be standardised. That way we really could write code that is decoupled from any particular [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.springactionscript.org/">Spring Actionscript</a>, <a href="http://github.com/sammyt/dawn">Dawn</a>, <a href="http://smartypants.expantra.net/">SmartyPants-IOC</a>, <a href="http://swizframework.org/">Swiz</a>, <a href="http://www.spicefactory.org/parsley/">Spicelib</a> (used by <a href="http://www.spicefactory.org/parsley/">Parsley</a>) and <a href="http://github.com/tschneidereit/SwiftSuspenders">SwiftSuspenders</a> (used by <a href="http://www.robotlegs.org/">Robotlegs</a>) perform Dependency Injection.</p>
<p>Moreover they can all perform automatic DI by parsing class metadata. It would be grand if the common DI related metadata tags could be standardised. That way we really could write code that is decoupled from any particular framework.</p>
<p>I&#8217;ve started a little document over at GitHub:</p>
<p><a href="http://github.com/darscan/AS3-DI-Metadata">http://github.com/darscan/AS3-DI-Metadata</a></p>
<p>I don&#8217;t know the libraries well enough to fill in all the details, so if you could fork the repo and edit the doc that would be great! Also, the format/layout could be better, so if you have any ideas on that front.. edit away.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2010/09/08/as3-di-metadata-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RobotLegs AS3: GitHub Wiki Updated</title>
		<link>http://shaun.boyblack.co.za/blog/2009/06/23/robotlegs-as3-github-wiki-updated/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/06/23/robotlegs-as3-github-wiki-updated/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 11:51:14 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=696</guid>
		<description><![CDATA[I&#8217;ve added a little page to the RobotLegs Wiki: RobotLegs Overview]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve added a little page to the <a title="RobotLegs AS3 Framework" href="http://shaun.boyblack.co.za/blog/robotlegs-as3/">RobotLegs</a> Wiki: <strong><a title="RobotLegs AS3 Overview" href="http://wiki.github.com/darscan/robotlegs/overview">RobotLegs Overview</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/06/23/robotlegs-as3-github-wiki-updated/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RobotLegs AS3: System/Context Event Flow</title>
		<link>http://shaun.boyblack.co.za/blog/2009/05/02/robotlegs-as3-systemcontext-event-flow/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/05/02/robotlegs-as3-systemcontext-event-flow/#comments</comments>
		<pubDate>Sat, 02 May 2009 01:20:53 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Banter]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=534</guid>
		<description><![CDATA[RobotLegs AS3 is a Dependency Injection Driven MVCS Framework for Flash and Flex inspired by PureMVC. Actors may be dependent on actors below them in the diagram, but should not be dependent on anything above them. Commands, Services and Proxies may dispatch system events, but should never listen for them. Mediators may both dispatch and [...]]]></description>
			<content:encoded><![CDATA[<p><em><a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://wiki.github.com/darscan/robotlegs">RobotLegs AS3</a> is a Dependency Injection Driven MVCS Framework for Flash and Flex inspired by <a title="PureMVC" href="http://puremvc.org/">PureMVC</a>.</em></p>
<p><img class="alignnone size-full wp-image-666" title="RobotLegs AS3 Event Flow" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/05/robotlegsas3_event_flow.png" alt="RobotLegs AS3 Event Flow" width="430" height="230" /></p>
<p>Actors may be dependent on actors below them in the diagram, but should not be dependent on anything above them.</p>
<p>Commands, Services and Proxies may dispatch system events, but should never listen for them.</p>
<p>Mediators may both dispatch and listen for system events.<span id="more-534"></span></p>
<p>So:</p>
<ol>
<li>Commands may depend on any number of Mediators, Services and Proxies, and may dispatch system events.</li>
<li>Mediators may depend on other Mediators, Services and or Proxies, and may dispatch and listen for system events.</li>
<li>Services may depend on Proxies, and may dispatch system events.</li>
<li>Proxies may depend on other Proxies, and may dispatch system events.</li>
</ol>
<h3>Why?</h3>
<p>Check out the PureMVC FAQ: <a title="PureMVC FAQ" href="http://puremvc.org/content/category/3/23/188/">http://puremvc.org/content/category/3/23/188/</a></p>
<h3>Prompted By:</h3>
<p>This <a href="http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/comment-page-1/#comment-3473">comment</a>, thanks!</p>
<p><strong>UPDATE</strong>: I somehow completely messed up the diagram when I last updated it.. I got the direction for the Service and Proxy event flow backwards.. Doh!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/05/02/robotlegs-as3-systemcontext-event-flow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Constructor Injection vs Setter Injection</title>
		<link>http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-vs-setter-injection/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-vs-setter-injection/#comments</comments>
		<pubDate>Fri, 01 May 2009 17:56:37 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Banter]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[smartypants-ioc]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=512</guid>
		<description><![CDATA[Constructor injection is theoretically superior: Constructor Injection vs Setter Injection Constructor vs Setter Injection &#8211; Constructor is Better Setter injection versus constructor injection and the use of required Before I built RobotLegs I was sold on constructor injection. My prototype, however, used SmartyPants-IOC which lacked constructor injection, so I bit my lip and used setter [...]]]></description>
			<content:encoded><![CDATA[<p>Constructor injection is theoretically superior:</p>
<p><a href="http://misko.hevery.com/2009/02/19/constructor-injection-vs-setter-injection/">Constructor Injection vs Setter Injection</a><br />
<a href="http://www.pbell.com/index.cfm/2006/11/18/Constructor-vs-Setter-Injection-Constructor-is-Better">Constructor vs Setter Injection &#8211; Constructor is Better</a><br />
<a href="http://blog.springsource.com/2007/07/11/setter-injection-versus-constructor-injection-and-the-use-of-required/">Setter injection versus constructor injection and the use of required</a></p>
<p>Before I built <a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">RobotLegs</a> I was sold on constructor injection. My prototype, however, used <a title="SmartyPants-IOC" href="http://code.google.com/p/smartypants-ioc/">SmartyPants-IOC</a> which lacked constructor injection, so I bit my lip and used setter injection. In practice I found that often, especially with framework actors, it was incredibly convenient.<span id="more-512"></span></p>
<h2>Effort</h2>
<p>Unless you use a code generator, constructor injection requires roughly 3 times the effort (more code) and is roughly 3 times more prone to human error than this form of setter injection:</p>
<pre>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SomeMediator<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> someView<span style="color: #000066; font-weight: bold;">:</span>SomeView<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; <span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> someProxy<span style="color: #000066; font-weight: bold;">:</span>SomeProxy<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; <span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> someService<span style="color: #000066; font-weight: bold;">:</span>ISomeService<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SomeMediator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
</pre>
<p>Constructor injection requires twice the number of declarations and an extra assignment step.</p>
<pre>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> SomeMediator<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> someView<span style="color: #000066; font-weight: bold;">:</span>SomeView<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> someProxy<span style="color: #000066; font-weight: bold;">:</span>SomeProxy<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> someService<span style="color: #000066; font-weight: bold;">:</span>ISomeService<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
&nbsp; <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> SomeMediator<span style="color: #000000;">&#40;</span> someView<span style="color: #000066; font-weight: bold;">:</span>SomeView<span style="color: #000066; font-weight: bold;">,</span> someProxy<span style="color: #000066; font-weight: bold;">:</span>SomeProxy<span style="color: #000066; font-weight: bold;">,</span> someService<span style="color: #000066; font-weight: bold;">:</span>ISomeService <span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">// Boilerplate...</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>someView = someView<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>someProxy = someProxy<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; &nbsp; <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>someService = someService<span style="color: #000066; font-weight: bold;">;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
</pre>
<h2>Forgetfulness</h2>
<p>Let&#8217;s consider what Misko deems the biggest problem with setter injection: forgetting dependencies.</p>
<p>In the case of setter injection, and in the context of the application itself (running on the <a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">RobotLegs</a> framework), if we forget to set up a dependency rule, we&#8217;ll get a runtime error when the DI framework tries to inject into the Mediator (which happens automatically). This would be exactly the same for Constructor injection. A draw.</p>
<p>If we are instantiating the Mediator manually (in a unit test for example), and we don&#8217;t manually inject into it with our DI framework, then setter injection is obviously weaker &#8211; we won&#8217;t get the DI RTE, we&#8217;ll just get a null-pointer exception at some point in our test. Constructor injection is much better here, but something Misko overlooks is that constructor injection is more prone to human error from the inside. We have at least three places where we can forget to properly take care of our dependencies:</p>
<ol>
<li>We might declare the dependency in our constructor argument list, but then forget to store the passed value inside our constructor.</li>
<li>We might declare a property on the class itself but then forget to ask for it in the ctor argument list.</li>
<li>We might even be so delirious as to incorrectly assign the values in our ctor by excluding &#8220;this.&#8221;.</li>
</ol>
<p>None of those mistakes will cause a compile-time error. As with setter injection, we&#8217;ll just get a RTE at some point. Again, a draw.</p>
<p>When we consider what a Mediator is, and the fact that Mediators are amongst the biggest candidates for re-factoring (we might start with one Mediator for a complex nested view, for example, and then break it out into several finely grained Mediators later), setter injection becomes even more convenient. Constructor injection requires more effort when dependencies change: 3 places need editing, including the constructor&#8217;s method signature &#8211; necessitating external changes for any manually instantiated instances.</p>
<h2>Inconsistent State</h2>
<p>Another charge against setter injection is that it can be responsible for objects in inconsistent states: directly after construction, and before injection has occurred, the object is not quite ready for use (it&#8217;s dependencies are missing). But this isn&#8217;t much of a problem when your architectural framework handles that side of things automatically &#8211; it instantiates and injects things for you.</p>
<h2>Hybrids In Progress, A Sliding Scale</h2>
<p>I&#8217;d also like to touch on the mixing of injection styles. Some people see this as akin to mixing coding standards in a project &#8211; ugly. At the same time, setter injection must be used under certain conditions (circular dependencies for example), so even purists have to mix styles sometimes. I&#8217;ve chosen to roll with it, and use what makes the most sense for the case at hand. Looking at actors in the <a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">RobotLegs</a> framework, I&#8217;m inclined towards:</p>
<ul>
<li>Setter injection for Mediators</li>
<li>Setter injection for Commands</li>
<li>Constructor injection for Proxies</li>
<li>Constructor injection for Services</li>
<li>Constructor injection for non-framework entities</li>
</ul>
<p>My reasoning is that:</p>
<ul>
<li>Mediators and Commands generally have more dependencies and require refactoring more frequently</li>
<li>Proxies and Services should have very few dependencies</li>
<li>Non-framework entities are more likely to be created manually</li>
</ul>
<p>If we look at setter vs ctor injection on a sliding scale, we have:</p>
<ul>
<li>setter -&gt; constructor</li>
<li>convenient -&gt; safe</li>
</ul>
<p>We can always migrate from setter to ctor injection as classes settle down and their refactoring likelihood decreases:</p>
<ul>
<li>setter -&gt; constructor</li>
<li>sketchy -&gt; final</li>
</ul>
<p>Mediators, by their very nature, are the sketchiest parts of an application: they are tightly coupled both to the application and the view. Commands are coupled to the application. There is very little need to make either of these actors &#8220;safe&#8221; or &#8220;final&#8221;, and for flexibility it is wise to leave them &#8220;convenient&#8221;.</p>
<p>Proxies and Services, however, should not be too coupled to the application. They should be the &#8220;safest&#8221; most &#8220;final&#8221; classes in the interests of loose coupling and portability. I would start with setter injection and migrate to constructor injection as those components mature.</p>
<h2>Prompted By:</h2>
<p>This <a href="http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/comment-page-1/#comment-3473">comment</a>, thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/05/01/constructor-injection-vs-setter-injection/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Another Architectural Framework, But Why?</title>
		<link>http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 12:47:15 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Banter]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[crybaby]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[smartypants-ioc]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=406</guid>
		<description><![CDATA[The State Of The Game There are some great Flash and Flex application frameworks out there right now. Mate, Swiz and PureMVC (update: and Parsley!) stand out. The authors of these frameworks realized that the Flash Platform is different enough to the JVM to warrant a fresh approach to application design. Someone mentioned recently that [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-458" title="robotlegssketchsmall" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/robotlegssketchsmall.gif" alt="robotlegssketchsmall" width="221" height="260" /></p>
<h2>The State Of The Game</h2>
<p>There are some great Flash and Flex application frameworks out there right now. <a title="Mate Flex Framework" href="http://mate.asfusion.com/">Mate</a>, <a title="The Swiz Framework" href="http://code.google.com/p/swizframework/">Swiz</a> and <a title="PureMVC Flash and Flex Framework" href="http://puremvc.org/">PureMVC</a> (update: and <a title="Parsley" href="http://www.spicefactory.org/parsley/">Parsley</a>!) stand out. The authors of these frameworks realized that the Flash Platform is different enough to the <a title="The Java Virtual Machine" href="http://en.wikipedia.org/wiki/Java_Virtual_Machine">JVM</a> to warrant a fresh approach to application design.</p>
<p><span id="more-406"></span>Someone mentioned recently that for such a young language (referring to Action Script 3), it&#8217;s quite surprising how many frameworks have sprung up around it. I&#8217;m not surprised at all: rich internet application development is complicated.. and it&#8217;s a pretty recent field in the grand scheme of things. It brings new (and often subtle) twists to traditional web and desktop software development. Writing elegant, maintainable code is hard enough as it is, but doing so on a rapidly evolving, heavily interactive platform.. well, people are still working on the best way to approach that (unless I missed the big news &#8211; in which case you can stop reading right here).</p>
<p>Hence all the groovy new architectural frameworks popping up. They share some common goals:</p>
<ol>
<li>Help developers write good, clean, understandable, testable, maintainable code</li>
<li>Help them write that code faster</li>
<li>Provide an easy-to-use mechanism for application/context/module subsystem inter-communication</li>
<li>Provide an easy-to-use mechanism for wiring distinct parts of the application together whilst keeping those parts loosely coupled</li>
</ol>
<p>And they succeed. To various degrees. I think they&#8217;re all pretty cool.</p>
<p><img class="alignnone size-full wp-image-439" title="scenery" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/scenery.gif" alt="scenery" width="450" height="370" /></p>
<h2>A Poke In The Back With A Sharp Stick</h2>
<h3>So why build another one?</h3>
<ol>
<li>For fun &#8211; it&#8217;s how I learn stuff</li>
<li>Because I find the other frameworks critically flawed</li>
</ol>
<h3>What kind of &#8220;critical&#8221; flaws?</h3>
<ol>
<li>Singletons (big &#8220;S&#8221;)</li>
<li>Central/Static Event Dispatchers</li>
<li>Using the Display List as an Application Event Bus</li>
<li>Casting</li>
<li>The Service/Model Locator Pattern</li>
<li>The Presenter Pattern</li>
<li>Injecting into View Components</li>
</ol>
<h3>And what&#8217;s wrong with those things?</h3>
<p>They are bad ideas:</p>
<h4>1. The Singleton &#8211; Bad Idea</h4>
<p>The standard <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">Singleton</a> <a href="http://misko.hevery.com/2008/08/21/where-have-all-the-singletons-gone/">implementation</a> (that static getInstance method) <a href="http://misko.hevery.com/2008/08/25/root-cause-of-singletons/">is</a> <a href="http://tech.puredanger.com/2007/07/03/pattern-hate-singleton/">severely</a> <a href="http://c2.com/cgi/wiki?SingletonsAreEvil">flawed</a>. Unless you are writing device drivers, there is almost no excuse to touch a Singleton with a sharp stick.</p>
<p>Singletons bring bags of hurt to frameworks and applications.. suddenly, trying to build a modular application becomes an overly complicated affair, and unit testing becomes a nightmare. Singletons are viruses &#8211; they infect every class they touch.</p>
<p>And Multitons? If Singletons are evil, Multitons are diabolically evil. What makes the Multiton pattern incredible is that it manages to chump the Singleton as the worst Design Pattern Of All Time &#8211; no easy feat, and certainly the work of an evil genius with a wicked sense of humour. Not only do we have all the problems of the classic Singleton, we throw a poorly designed Model Locator into the mix &#8211; to pull out these horrendous Singletons we now need to use String keys.</p>
<p>Contextual singletons (as made possible by Dependency Injection frameworks) are a much nicer solution, they don&#8217;t require any extra (fluffy, nonsense, ill placed) code on the target classes themselves, and they allow identical applications/modules to exist side-by-side in the Virtual Machine without fighting and mass confusion. Which leads me to..</p>
<h4>2. Central/Static Event Dispatchers &#8211; Bad Idea</h4>
<p>As convenient as it is to use, a Central Dispatcher is a really bad idea. Another Singleton effectively. Pop two apps into the same SWF and watch, with excitement and glee, all the cross-talk that occurs &#8211; both apps responding to each other&#8217;s events. Oh noes!</p>
<p>Architectural frameworks generally provide you with an application event bus/channel/dispatcher to &#8220;chat&#8221; on, but this should be localised to the application itself (or some other context). Which leads me to&#8230;</p>
<h4>3. Using the Display List as an Application Event Bus &#8211; Bad Idea</h4>
<p>I find this one fairly disturbing too: dispatching system/application events along the display list. No!</p>
<p>A View is just that: &#8220;<strong>A</strong> view&#8221;, not &#8220;<strong>The</strong> view&#8221;. And don&#8217;t get me started on those innocent looking Event Maps! No, let&#8217;s move along&#8230;</p>
<h4>4. Casting &#8211; Bad Idea</h4>
<p>Besides wasting space and annoying programmers, casting distributes knowledge throughout a system &#8211; casting requires that you look elsewhere in your codebase for the &#8220;real&#8221; type.</p>
<p>And it weakens our lovely compile-time type checking (albeit temporarily), sending us back in time to our unhappy days building complex applications in the Flash IDE. And it&#8217;s boring. And it often occurs because of..</p>
<h4>5. The Service/Model Locator Pattern &#8211; Bad Idea</h4>
<p>Classes should not be responsible for &#8220;fetching&#8221; their dependencies &#8211; this is a separate responsibility that belongs somewhere else entirely &#8211; they should only contain code for doing their job. When classes reach outside to fetch things from the application/context they sneakily hide their dependencies away from the outside world.</p>
<p>Unit testers will despise you (which is a serious problem if you are writing your own tests) because it becomes incredibly hard to figure out how much set up needs to be done to write even the smallest of tests. You have to look inside the class for any calls to the Service Locator to find your direct dependencies, and then you have to look inside each of those classes to find theirs and so on. No no, that&#8217;s just cruel.</p>
<h4>6. The Presenter Pattern &#8211; Bad Idea</h4>
<p>Ok, it&#8217;s going to be tricky to back this one up. But think about it this way: the Presenter pattern requires that you modify your View component (to add a reference to a Presenter). Not a biggie, but once you&#8217;ve done that you&#8217;ve started coupling your View component to your application/context &#8211; a slippery slope.</p>
<p>I think the Mediator pattern is a much better fit for Flash and it&#8217;s Display List, and even more-so when it comes to Flex (and other UI frameworks with complex component life cycles).</p>
<p>It is far easier and cleaner to &#8220;wrap&#8221; around view components and poke their APIs than to have to modify (extend) them to make space for their Presenter references. Leave those components alone, leave them to the component developers, you&#8217;re supposed to be writing an application. While we&#8217;re on the Presenter pattern, let&#8217;s take a look at something dirty one might do in an attempt to salvage it&#8217;s reputation..</p>
<h4>7. Injecting into View Components &#8211; Bad Idea</h4>
<p>Besides being incredibly slow and wasteful (especially for Flex UIComponents), injecting directly into a View component couples it to your application/context.</p>
<p>View components, with any hope for reuse, should be as self-contained as possible. They should expose an API and dispatch Events. They shouldn&#8217;t be dependent on the application they are in or the framework it happens to be using. Give &#8216;em ViewModels (M-V-VM) if you must, but don&#8217;t go and couple those ViewModels to your application, and certainly don&#8217;t inject them.</p>
<p><img class="alignnone size-full wp-image-467" title="ducksaysyuk1" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/ducksaysyuk1.gif" alt="ducksaysyuk1" width="450" height="632" /></p>
<h2>Wrapping Up This Micro-Post</h2>
<h4>Well that&#8217;s just your opinion, dude</h4>
<p>Yeh, but I&#8217;m pretty proud of my little 7 point list there (cos I&#8217;m an OOP noob, and this stuff gets me amped). Using Dependency Injection I was able to avoid all those nasty things when building my own PureMVC-like framework.</p>
<h4>But that just shifts the problem somewhere else</h4>
<p>Yes, somewhere more appropriate.</p>
<p>Besides some convenient implementations (in the mvcs package), and some marker interfaces (in the core package) <a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">RobotLegs</a> essentially provides two things:</p>
<ol>
<li>A Command Factory &#8211; for binding Commands to Events, and</li>
<li>A Mediator Factory &#8211; for handling automatic Mediator creation and registration</li>
</ol>
<p>Everything else is handled by a Dependency Injection framework and an Event Dispatcher.</p>
<p><img class="alignnone size-full wp-image-459" title="theendissmall" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/theendissmall.gif" alt="theendissmall" width="225" height="152" /></p>
<p><strong>Note</strong>: If you&#8217;ve been through all this before, I apologise, it&#8217;s new to me.. I&#8217;m a little slow.</p>
<p><strong>Beware</strong>: the current default implementation of <a title="RobotLegs AS3" href="http://shaun.boyblack.co.za/blog/robotlegs-as3/">RobotLegs</a> makes use of annotated setter injection. This breaks encapsulation, is open to misuse, and runs the risk of leaving objects in partially initialised states, but it&#8217;s very convenient! You can swap out SmartyPants-IOC for a Dependency Injection framework that performs constructor injection if you really want to play it safe.</p>
<p><strong>I&#8217;ll admit straight-up</strong> (albeit as a footnote) that I never really gave Cairngorm much of a chance &#8211; preliminary research led me to demos, documentation and diagrams that screamed poor design and foreshadowed immense struggle. I&#8217;m sure there is a way to build a well designed Cairngorm application, but it looks like it&#8217;d take an awful amount of effort.. and code. And it probably wouldn&#8217;t be very much fun.</p>
<p><strong>UPDATE</strong>: Removed some words that made me sound like a douche bag.</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/04/29/another-architectural-framework-but-why/feed/</wfw:commentRss>
		<slash:comments>39</slash:comments>
		</item>
		<item>
		<title>RobotLegs AS3 v0.2: ReversiblePants</title>
		<link>http://shaun.boyblack.co.za/blog/2009/04/22/robotlegs-as3-v02-reversiblepants/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/04/22/robotlegs-as3-v02-reversiblepants/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 21:35:00 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[smartypants-ioc]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=353</guid>
		<description><![CDATA[RobotLegs AS3 is a Dependency Injection Driven MVCS Framework for Flash and Flex inspired by PureMVC. RobotLegs AS3 v0.2 (codename ReversiblePants) is up on GitHub: Now with DI/IOC and Reflection adapters instead of direct dependencies on the SmartyPants-IOC framework. By providing your own adapters you can run RobotLegs off a DI/IOC/Reflection framework of your choosing. [...]]]></description>
			<content:encoded><![CDATA[<p><em>RobotLegs AS3 is a Dependency Injection Driven MVCS Framework for Flash and Flex inspired by <a title="PureMVC" href="http://puremvc.org/">PureMVC</a>.</em></p>
<p><strong><a title="RobotLegs AS3" href="http://wiki.github.com/darscan/robotlegs">RobotLegs AS3</a> v0.2 (codename ReversiblePants) is up on <a title="GitHub" href="http://github.com/">GitHub</a>:</strong></p>
<p>Now with DI/IOC and Reflection adapters instead of direct dependencies on the <a title="SmartyPants-IOC" href="http://code.google.com/p/smartypants-ioc/">SmartyPants-IOC framework</a>. By providing your own adapters you can run RobotLegs off a DI/IOC/Reflection framework of your choosing.<span id="more-353"></span></p>
<p><strong>More info (and updated demo):</strong></p>
<p><a title="RobotLegs AS3 - DI Driven MVCS Framework for Flash and Flex" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/</a></p>
<p>I should point out that RobotLegs AS3 is still very much a proof-of-concept framework. It works, but it&#8217;s architectural design might change a bit on the road to v1.0. That said, I&#8217;m using it underneath a large-scale, real-world project, and it&#8217;s working great!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/04/22/robotlegs-as3-v02-reversiblepants/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>RobotLegs AS3: A DI Driven MVCS Framework for Flash &amp; Flex &#8211; Inspired by PureMVC</title>
		<link>http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 03:07:53 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[mvcs]]></category>
		<category><![CDATA[puremvc]]></category>
		<category><![CDATA[smartypants-ioc]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=314</guid>
		<description><![CDATA[Want a framework like PureMVC but without Singletons, Service Locators, or casting? Perhaps one with Dependency Injection and Automatic Mediator Registration? Well, you might enjoy RobotLegs AS3: yet another lightweight micro-architecture for Rich Internet Applications. It&#8217;s got the bits that I like about PureMVC (Mediators, Commands and Proxies) without any of the bits that I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://wiki.github.com/darscan/robotlegs"><img class="alignleft size-full wp-image" style="margin: 0px 10px 0px 0pt;" title="RobotLegs AS3" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/robotlegslogotiny.png" alt="RobotLegs AS3" width="50" height="50" /></a>Want a framework like PureMVC but without Singletons, Service Locators, or casting? Perhaps one with Dependency Injection and Automatic Mediator Registration?</p>
<p>Well, you might enjoy RobotLegs AS3: yet another lightweight micro-architecture for Rich Internet Applications.</p>
<p><span id="more-314"></span>It&#8217;s got the bits that I like about PureMVC (Mediators, Commands and Proxies) without any of the bits that I&#8217;m not so fond of (Service Locator, Singletons, casting casting casting!).</p>
<p>I&#8217;ve put the source up on GitHub, so feel free to Fork it and make it better:</p>
<p><a title="Robotlegs on GitHub" href="http://github.com/robotlegs/robotlegs-framework">http://github.com/robotlegs/robotlegs-framework</a></p>
<p>Discussion Group:</p>
<p><a title="RobotLegs AS3 Issues" href="http://github.com/darscan/robotlegs/issues"></a><a title="RobotLegs AS3 Discussion Group" href="http://groups.google.com/group/robotlegs">http://groups.google.com/group/robotlegs</a></p>
<p>Here&#8217;s a little demo:<br />
(<strong>Please note</strong>: the API and usage has cleaned up a lot since this demo was built)</p>
<p><a href="http://examples.robotlegs.org/helloflex/"><img class="alignnone size-full wp-image-327" title="RobotLegs Demo App: HelloFlex" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/helloflex460.png" alt="RobotLegs Demo App: HelloFlex" width="460" height="460" /></a></p>
<p>You can also view the demo <a title="RobotLegs Demo - HelloFlex" href="http://examples.robotlegs.org/helloflex/">here</a> (with view source).</p>
<p>The demo app is perhaps not so great, but it demonstrates many of the framework&#8217;s features. AND, there isn&#8217;t a single cast in there! No actor casting, no payload casting, not any casting, not even for fun!</p>
<p>I have much to say about this little framework, but it&#8217;s the wrong side of 5am and I&#8217;ve had a crazy long day. In the meantime, you can find some background info <a title="AS3 Dependency Injection and [Autowire]" href="http://shaun.boyblack.co.za/blog/2009/03/13/as3-dependency-injection-and-autowire/">here</a> and <a title="Flexible RIA Architecture" href="http://shaun.boyblack.co.za/blog/2009/02/08/flexible-ria-architecture-puremvc-and-mate/">here</a> if you&#8217;re interested.</p>
<p><strong>Update: <a title="Robotlegs AS3" href="http://www.robotlegs.org/">RobotLegs Lives!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
		<item>
		<title>AS3 Dependency Injection Framework SmartyPantsIOC &#8211; Released!</title>
		<link>http://shaun.boyblack.co.za/blog/2009/04/14/as3-dependency-injection-framework-smartypantsioc-released/</link>
		<comments>http://shaun.boyblack.co.za/blog/2009/04/14/as3-dependency-injection-framework-smartypantsioc-released/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 10:52:22 +0000</pubDate>
		<dc:creator>shaun</dc:creator>
				<category><![CDATA[Banter]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[Robotlegs]]></category>
		<category><![CDATA[smartypants-ioc]]></category>

		<guid isPermaLink="false">http://shaun.boyblack.co.za/blog/?p=309</guid>
		<description><![CDATA[Great news: SmartyPantsIOC, a Dependency Injection framework for Flash and Flex has been released. Check it out here: http://code.google.com/p/smartypants-ioc/ I guess that means that it&#8217;s time for me to release RobotLegs &#8211; an MVCS micro-architecture for Rich Internet Applications inspired by PureMVC, Mate and Swiz! But first, I have to write a little demo application [...]]]></description>
			<content:encoded><![CDATA[<p>Great news: SmartyPantsIOC, a Dependency Injection framework for Flash and Flex has been released. Check it out here:</p>
<p><a title="SmartyPantsIOC" href="http://code.google.com/p/smartypants-ioc/">http://code.google.com/p/smartypants-ioc/</a></p>
<p><img class="alignnone size-full wp-image-311" title="SmartyPantsIOC" src="http://shaun.boyblack.co.za/blog/wp-content/uploads/2009/04/smartypantslogo.png" alt="SmartyPantsIOC" width="153" height="55" /></p>
<p>I guess that means that it&#8217;s time for me to release <a title="RobotLegs AS3 Announcement" href="http://shaun.boyblack.co.za/blog/2009/04/16/robotlegs-an-as3-mvcs-framework-for-flash-and-flex-applications-inspired-by-puremvc/">RobotLegs</a> &#8211; an MVCS micro-architecture for Rich Internet Applications inspired by PureMVC, Mate and Swiz! But first, I have to write a little demo application for it.. coming soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://shaun.boyblack.co.za/blog/2009/04/14/as3-dependency-injection-framework-smartypantsioc-released/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
