<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Building Blocks &#187; object oriented concepts</title>
	<atom:link href="http://joelhooks.com/category/object-oriented-concepts/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelhooks.com</link>
	<description>[without unit tests] you&#039;re not refactoring; you&#039;re just changing shit. -Hamlet D&#039;Arcy</description>
	<lastBuildDate>Fri, 30 Dec 2011 04:11:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
		<item>
		<title>Robotlegs, AS3-Signals and the SignalCommandMap Example</title>
		<link>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/</link>
		<comments>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 21:39:53 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=432</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Robotlegs and AS3-Signals play really well together. Both apply solid object-oriented principles to accomplish their respective goals. Signals is extremely well suited for automated Dependency Injection. By combining Signals and Robotlegs you are able to eliminate the use of Flash Events in the framework layer of your application. Eliminating Events means eliminating the ambiguity that [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://robotlegs.org">Robotlegs</a> and <a href="http://github.com/robertpenner/as3-signals">AS3-Signals</a> play really well together. Both apply solid object-oriented principles to accomplish their respective goals. Signals is extremely well suited for automated Dependency Injection. By combining Signals and Robotlegs you are able to eliminate the use of Flash Events in the framework layer of your application. Eliminating Events means eliminating the ambiguity that can accompany Events and their String registry based approach to the <a href="http://en.wikipedia.org/wiki/Observer_pattern">Observer pattern</a>. Signals provides a strongly-typed object-oriented approach to this same pattern.</p>
<p>With the standard <a href="http://wiki.github.com/robotlegs/robotlegs-framework/best-practices">Robotlegs MVCS implementation</a> you leverage the events provided by Actionscript 3 to communicate amongst the various actors of an application. From models and services dispatching notifications of their actions to triggering commands, events are a core piece of the implementation. To facilitate the use of Signals within MVCS it was necessary to create an extension to allow for Signals to be registered as Command triggers. This need spawned the <a href="http://github.com/joelhooks/signals-extensions-CommandSignal">SignalCommandMap utility</a>.</p>
<p>The SignalCommandMap extends the normal MVCS context and creates a SignalContext. The SignalContext instantiates and provides access to the SignalCommandMap alongside the other maps that are standard to Robotlegs. The SignalCommandMap allows you to map Signal classes and instances to commands that will be executed when the Signal dispatch() method is called. The value objects that are passed in the dispatch are then injected into the command alongside any other mapped injections you have created.</p>
<p>Let's take a look at a simple example that makes use of the SignalCommandMap and discuss some of the underlying code to see how it works:</p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_index_1921891680"
			class="flashmovie"
			width="320"
			height="550">
	<param name="movie" value="http://joelhooks.com/examples/robotlegsSignalsCafe/index.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/robotlegsSignalsCafe/index.swf"
			name="fm_index_1921891680"
			width="320"
			height="550">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><a href="http://github.com/joelhooks/robotlegs-examples-RobotlegsSignalsCafe">Source available on Github</a> (<a href="http://github.com/joelhooks/robotlegs-examples-RobotlegsSignalsCafe/zipball/v0.2.4212342">zip</a>)<br />
<span id="more-432"></span><br />
The example is a menu that allows you to add food which is displayed in a list. The total cost of your selected items is displayed and you can remove items from your order. First let's get started by taking a look at the context of the Application, which extends SignalContext:</p>
<h6>SignalCafeContext.as Bootstraps the Application</h6>

<div class="wp_codebox"><table><tr id="p4325"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p432code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SignalCafeContext <span style="color: #0066CC;">extends</span> SignalContext
<span style="color: #66cc66;">&#123;</span>
    override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
    <span style="color: #66cc66;">&#123;</span>
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodOrderModel<span style="color: #66cc66;">&#41;</span>;
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodOrderUpdated<span style="color: #66cc66;">&#41;</span>;
      injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>FoodItemAddedToOrder<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>AddFoodItemToOrder, AddFoodItemToOrderCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>FoodItemSelected, FoodItemSelectedCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>RemoveAllOfSelectedItem, RemoveAllSelectedItemCommand<span style="color: #66cc66;">&#41;</span>;
      signalCommandMap.<span style="color: #006600;">mapSignalClass</span><span style="color: #66cc66;">&#40;</span>NoFoodItemSelected, NoFoodItemSelectedCommand<span style="color: #66cc66;">&#41;</span>;
&nbsp;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodSelectionView, FoodSelectionViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>CurrentOrderView, CurrentOrderViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodOrderSummaryView, FoodOrderSummaryViewMediator<span style="color: #66cc66;">&#41;</span>;
      mediatorMap.<span style="color: #006600;">mapView</span><span style="color: #66cc66;">&#40;</span>FoodItemRemovalView, FoodItemRemovalViewMediator<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>A SignalContext is structured exactly the same as a standard Robotlegs MVCS Context class. You override the startup() method and bootstrap your application. The major difference here is that you are mapping signals to commands instead of mapping events to commands. With the SignalCommandMap you are not restricted to just using Signals for triggering commands. You could mix events and Signals liberally as your needs or requirements dictated. As a warning, this could quickly became confusing and choosing one or the other might be a saner choice.</p>
<p>SignalCafeContext is mapping 4 Signals to 4 commands. The Signals are typed extensions of the base Signal class. This is necessary to provide Robotlegs (and more specifically in this case SwiftSuspenders) object types to differentiate for the purposes of injection. You could use named injections, but this would rapidly riddle your application with strings that would need to be laboriously checked for accuracy. Avoiding strings in favor of compiler-checked object types is always a good choice.</p>
<p>The strongly typed Signals are relatively simple. They contain a constructor and no additional methods or properties are added to the Signal. You can of course add properties and methods to the extended Signals,, but it wasn't necessary for this example. The SignalCommandMap will accept any class that implements the ISignal interface. Let's take a look at one of the commands triggered by a Signal mapped in the SignalCafeContext:</p>
<h6>AddFoodItemToOrder.as triggers the AddFoodItemToOrderCommand</h6>

<div class="wp_codebox"><table><tr id="p4326"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p432code6"><pre class="actionscirpt" style="font-family:monospace;">public class AddFoodItemToOrder extends Signal
{
	public function AddFoodItemToOrder()
	{
		super(FoodType);
	}
}</pre></td></tr></table></div>

<p>That is the whole of the extended Signal. As mentioned, it is not very complex at all. You will notice that super(FoodType) is being called. Signals take constructor arguments consisting of classes or interfaces that will be used as the value objects transmitted via the dispatch() method. In this case we are using FoodType as this value to ensure that our Signal will carry the payload that the mapped command is expecting.</p>
<p>Command signals are named a bit differently than standard reaction Signals. A Signal that is triggering a command is "requesting action" or specifying an action that needs to occur. Standard signals are typically past tense, or informative, describing an action that has occurred. By using this standard it is much easier to differentiate quickly what each Signal's purpose is. This can help with overall clarity within your application.</p>
<p>The AddFoodItemToOrder signal is dispatched by the FoodSelectionViewMediator which is mediating the DropDownList of FoodTypes and the "Add Some Food" button:</p>
<h6>FoodSelectionViewMediator.as mediates a view and dispatches a Signal when the user has clicked the "Add Some Food" button</h6>

<div class="wp_codebox"><table><tr id="p4327"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p432code7"><pre class="actionscript" style="font-family:monospace;">	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FoodSelectionViewMediator <span style="color: #0066CC;">extends</span> Mediator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> view:FoodSelectionView;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> addItem:AddFoodItemToOrder;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			view.<span style="color: #006600;">itemTypeAdded</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>handleItemTypeAdded<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleItemTypeAdded<span style="color: #66cc66;">&#40;</span>itemType:FoodType<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addItem.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span>itemType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When the AddFoodItemToOrder Signal's dispatch(aFoodTypeInstance) is called the AddFoodItemToOrderCommand is triggered:</p>
<h6>AddFoodItemToOrderCommand.as is executed in response to AddFoodItemToOrder Signal dispatch</h6>

<div class="wp_codebox"><table><tr id="p4328"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p432code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AddFoodItemToOrderCommand <span style="color: #0066CC;">extends</span> SignalCommand
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> itemType:FoodType;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> model:FoodOrderModel;
&nbsp;
	override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		model.<span style="color: #006600;">addItemToOrder</span><span style="color: #66cc66;">&#40;</span>itemType<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>AddFoodItemToOrderCommand has two public properties that are marked for injection. The itemType property is a FoodType object. As you will recall, FoodType is the parameter that was passed to the AddFoodItemToOrder Signal super constructor. The AddFoodItemToOrder Signal dispatch() included an instance of a FoodType object which will be injected into the AddFoodItemToOrderCommand alongside any other injections that have been mapped and specified. In this case we are also injecting the FoodOrderModel instance that was mapped as a singleton in the SignalCafeContext.</p>
<div class="note-block">It is important to note that the parameters passed via the Signals dispatch() method will be instantly mapped and unmapped when the command is executed. If you've previously mapped a class that is being delivered via a Signal command mapping, that mapping will be overwritten and removed. To avoid this, favor using typed value objects as Signal parameters.</div>
<p>The AddFoodItemToOrderCommand has a simple job. It accesses the FoodOrderModel instance and calls its addItemToOrder method adding the FoodType parameter that was dispatched with the Signal to trigger the command.</p>
<p>That covers the basics of using the SignalCommandMap within a Robotlegs application. There are options that this example doesn't cover. I would recommend looking through the SignalCommandMap unit test suite for a thorough review of its capabilities. There are several more commands and Signals in the example for you to look over as well. Additionally the example uses Signals within views and the model to dispatch notifications. Outside of binding and UI events, no events are being passed within the application.</p>
<p>Hopefully this will give you the basic understanding of using the SignalCommandMap in a Robotlegs application. Let me know if you have any criticisms, comments, suggestions, or ideas regarding the utility's implementation at <a href="http://knowledge.robotlegs.org">http://knowledge.robotlegs.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/02/14/robotlegs-as3-signals-and-the-signalcommandmap-example/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs Image Gallery Example using AS3-Signals and the Presentation Model</title>
		<link>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/</link>
		<comments>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/#comments</comments>
		<pubDate>Sat, 16 Jan 2010 22:56:36 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=386</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Jason Crist posted a thought provoking request for his upcoming presentation comparing Robotlegs and Swiz. He's got a clever knack for stirring the framework ant pile and getting developers eyes off their IDEs long enough to discuss their passions. In this case the developers include Ben Clinkinbeard, Shaun Smith, Jesse Warden and myself. These conversations [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>Jason Crist <a href="http://pbking.com/blog/?p=209">posted a thought provoking request</a> for his upcoming presentation comparing <a href="http://www.robotlegs.org">Robotlegs</a> and <a href="http://swizframework.org/">Swiz</a>. He's got a clever knack for stirring the framework ant pile and getting developers eyes off their IDEs long enough to discuss their passions. In this case the developers include <a href="http://www.benclinkinbeard.com/">Ben Clinkinbeard</a>, <a href="http://shaun.boyblack.co.za/blog/">Shaun Smith</a>, <a href="http://jessewarden.com/">Jesse Warden</a> and myself.</p>
<p>These conversations are always good natured. While we work on (or use) "competing" frameworks there is always a sense of mutual respect. We like our tools but have obvious inclinations towards the projects that we've invested our hearts and souls into.</p>
<p>Ben Clinkinbeard has pointed out (several times!) that Robotlegs is all about extending the <a href="http://github.com/robotlegs/robotlegs-framework/tree/master/src/org/robotlegs/mvcs/">MVCS classes</a>. My stock answer is that there is a clear separation between the framework and the concrete MVCS implementation. To paraphrase Ben, "Well show it to me then!"</p>
<p>All of the <a href="http://www.robotlegs.org/examples/">"official" Robotlegs examples</a> are making use of the MVCS implementation. Why? Because it is solid, recognizable, and fairly easy to get one's head around. It provides a common ground for developers and a set architectural structure which is a huge advantage in any team environment. It is important to make the distinction  between the framework and the MVCS implementation. What does that even mean? At the core, Robotlegs is a modular set of tools to provide a convenient mechanism for wiring applications. Robotlegs is not doing class reflection. Robotlegs is not an automated dependency injection solution. Robotlegs is  an adapter to a dependency injection solution,by default the lightweight <a href="http://github.com/tschneidereit/SwiftSuspenders">SwiftSuspenders library</a>. Through a set of tools, namely the MediatorMap, CommandMap, ViewMap, and the injection adapter Robotlegs provides a robust starting place to begin coding your application.</p>
<p>The MVCS implementation is a set of base classes loosely modeled on <a href="http://puremvc.org/">PureMVC</a>. At the heart of the implementation is the Context. The Context creates instances of the various mapping classes, the injection adapter, and gives the developer a centralized IEventDispatcher that can be used for messaging between application tiers.. The other three classes, Actor, Mediator, and Command, reduce boiler plate and provide convenient access to injected dependencies typically used in the the MVCS tiers.</p>
<p>What if you hate PureMVC, don't want to extend any framework classes, or generally just want to work in a different way outside of a prescribed MVCS architecture?</p>
<p>No problem.</p>
<p>Here's the deal. Robotlegs, the framework, is a <a href="http://github.com/robotlegs/robotlegs-framework/tree/master/src/org/robotlegs/core/">set of interfaces</a>. You can effectively do whatever you want with these interfaces. You can make use of the base concrete implementations of the core interfaces, use a class from the MVCS implementation, implement your own concrete classes based on the framework core, bring in other libraries or any combination you can think of. In terms of a framework, Robotlegs can be whatever you want/need it to be.</p>
<h2>Man Joel, that was a long intro. Where's the freaking code??!?</h2>
<p><span id="more-386"></span><br />
So, to that end, I sat down to rework <a href="http://joelhooks.com/2009/07/17/robotlegs-as3-a-dependency-injection-driven-mvcs-framework-for-flashflex-–-inspired-by-puremvc/">my first (and still favorite) Robotlegs example application</a> and demonstrate this concept a bit.</p>
<p>I've stripped the example down slightly, using just the XMLImageService and not connecting to Flickr (in an effort to keep it simple).</p>
<p><a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM">Get the source on GitHub</a><br />
<a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM/zipball/v1.0.0"><strong>Download the full source as a Zip</strong></a></p>
<p><strong>This example requires:</strong><br />
<a href="http://www.robotlegs.org/">Robotlegs</a><br />
<a href="http://github.com/robertpenner/as3-signals">AS3-Signals</a><br />
<a href="http://github.com/joelhooks/signals-extensions-CommandSignal">SignalsCommandMap library</a><br />
<a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">Flex 4 SDK (a fairly recent version)</a></p>
<div class="note-block" align="center"><strong>You neeed <a href="http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4">SDK version 4.0.0.12635 or later</a> to compile this...</strong></div>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_index_703306585"
			class="flashmovie"
			width="400"
			height="500">
	<param name="movie" value="http://joelhooks.com/examples/robotlegsGalleryPM/index.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/robotlegsGalleryPM/index.swf"
			name="fm_index_703306585"
			width="400"
			height="500">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><br/><br />
The example should be familiar if you've looked at the Image Gallery previously. It loads images from a service and displays them. This version is much different on the code level however. Instead of mediators and data models, the gallery is using presentation models and AS3-Signals. It still uses the Context from the MVCS implementation. it's so f'n handy and I don't want to manually instantiate the maps.</p>
<h3>ImageGalleryContext</h3>

<div class="wp_codebox"><table><tr id="p38617"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p386code17"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageGalleryContext <span style="color: #0066CC;">extends</span> Context
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">private</span> const VIEW_PACKAGE:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;org.robotlegs.examples.imagegallery.view.components&quot;</span>;
	<span style="color: #0066CC;">private</span> const LOAD_GALLERY:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;loadGallery&quot;</span>;
&nbsp;
	override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//map the views</span>
		viewMap.<span style="color: #006600;">mapPackage</span><span style="color: #66cc66;">&#40;</span>VIEW_PACKAGE<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the presentation models</span>
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryViewPresentationModel, GalleryViewPresentationModel <span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryThumbnailsPresentationModel, GalleryThumbnailsPresentationModel <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the services and their factories</span>
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryImageService, XMLImageService <span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingletonOf</span><span style="color: #66cc66;">&#40;</span> IGalleryFactory, XMLGalleryFactory <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the signals</span>
		injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>GalleryUpdatedSignal<span style="color: #66cc66;">&#41;</span>;
		injector.<span style="color: #006600;">mapSingleton</span><span style="color: #66cc66;">&#40;</span>GalleryImageSelectedSignal<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//map the command</span>
		commandMap.<span style="color: #006600;">mapEvent</span><span style="color: #66cc66;">&#40;</span> LOAD_GALLERY, LoadGalleryCommand <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//go!!</span>
		eventDispatcher.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>LOAD_GALLERY<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see here the context is performing the duty of mapping the injections the application will use.</p>
<h3>Automated Dependency Injected View</h3>

<div class="wp_codebox"><table><tr id="p38618"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p386code18"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>s:Group xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
		 xmlns:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
		 xmlns:mx=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span> <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100&quot;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>fx:Script<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
			<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">examples</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">IGalleryThumbnailsPresentationModel</span>;
&nbsp;
			<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _model:IGalleryThumbnailsPresentationModel;
&nbsp;
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:IGalleryThumbnailsPresentationModel
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> _model;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
			<span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
			<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> model<span style="color: #66cc66;">&#40;</span>value:IGalleryThumbnailsPresentationModel<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
			<span style="color: #66cc66;">&#123;</span>
				_model = value;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>fx:Script<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>s:Rect <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
			<span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:fill<span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:SolidColor <span style="color: #0066CC;">color</span>=<span style="color: #ff0000;">&quot;#111111&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:fill<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>s:Rect<span style="color: #66cc66;">&gt;</span>
	<span style="color: #66cc66;">&lt;</span>s:Group <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:VerticalLayout gap=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">/&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:DataGroup id=<span style="color: #ff0000;">&quot;dgThumbnails&quot;</span>
					 clipAndEnableScrolling=<span style="color: #ff0000;">&quot;true&quot;</span>
					 dataProvider=<span style="color: #ff0000;">&quot;{model.dataProvider}&quot;</span>
					 <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
					 <span style="color: #0066CC;">height</span>=<span style="color: #ff0000;">&quot;85&quot;</span>
					 itemRenderer=<span style="color: #ff0000;">&quot;org.robotlegs.examples.imagegallery.view.components.renderers.GalleryImageThumbnailItemRenderer&quot;</span><span style="color: #66cc66;">&gt;</span>
			<span style="color: #66cc66;">&lt;</span>s:layout<span style="color: #66cc66;">&gt;</span>
				<span style="color: #66cc66;">&lt;</span>s:HorizontalLayout gap=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #66cc66;">/&gt;</span>
			<span style="color: #66cc66;">&lt;/</span>s:layout<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;/</span>s:DataGroup<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&lt;</span>s:HScrollBar id=<span style="color: #ff0000;">&quot;thumbScrollBar&quot;</span>
					  viewport=<span style="color: #ff0000;">&quot;{dgThumbnails}&quot;</span>
					  <span style="color: #0066CC;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>
					  smoothScrolling=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #66cc66;">/&gt;</span>
	<span style="color: #66cc66;">&lt;/</span>s:Group<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>s:Group<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>The viewMap is set to map the entire components folder. This is a convenient way to provide automated dependency injection to a large number of views. It also maps views in any sub-package of the mapped package.</p>
<h3>GalleryViewThumbnailsPresentationModel</h3>

<div class="wp_codebox"><table><tr id="p38619"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p386code19"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GalleryThumbnailsPresentationModel <span style="color: #0066CC;">implements</span> IGalleryThumbnailsPresentationModel
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> updated:GalleryUpdatedSignal;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> imageSelected:GalleryImageSelectedSignal;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>PostConstruct<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> mapSignalListeners<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		updated.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>galleryUpdated<span style="color: #66cc66;">&#41;</span>;
		imageSelected.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>updateImageSelectionState<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _dataprovider:ArrayCollection;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> dataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ArrayCollection
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> _dataprovider;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> dataProvider<span style="color: #66cc66;">&#40;</span>v:ArrayCollection<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		_dataprovider = v;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> galleryUpdated<span style="color: #66cc66;">&#40;</span>gallery:Gallery<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		dataProvider = gallery.<span style="color: #006600;">photos</span>;
		<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>gallery.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			imageSelected.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span>gallery.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> updateImageSelectionState<span style="color: #66cc66;">&#40;</span>image:GalleryImage<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> galleryImage:GalleryImage <span style="color: #b1b100;">in</span> dataProvider<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			galleryImage.<span style="color: #006600;">selected</span> = galleryImage == image;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>IGalleryViewThumbnailsPresentationModel</h3>

<div class="wp_codebox"><table><tr id="p38620"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p386code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * This interface is simple because the application is simple. Obviously in
 * a large application you'd get more complex interfaces.
 */</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGalleryThumbnailsPresentationModel
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> dataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ArrayCollection;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The presentation models are provided as singletons. The application will only display one of each view that requires the model. They are also mapped as interfaces with the interfaces being injected into the views. With the presentation model, the view does not update the model directly. By using interfaces we can supply read-only contracts between the presentation models and the views they control:</p>
<h3>XMLImageService</h3>

<div class="wp_codebox"><table><tr id="p38621"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p386code21"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> XMLImageService <span style="color: #0066CC;">implements</span> IGalleryImageService
<span style="color: #66cc66;">&#123;</span>
	protected <span style="color: #0066CC;">static</span> const BASE_URL:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;assets/gallery/&quot;</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> galleryFactory:IGalleryFactory;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> galleryUpdated:GalleryUpdatedSignal;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> XMLImageService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> service:HTTPService = <span style="color: #000000; font-weight: bold;">new</span> HTTPService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> responder:Responder = <span style="color: #000000; font-weight: bold;">new</span> Responder<span style="color: #66cc66;">&#40;</span>handleServiceResult, handleServiceFault<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">var</span> token:AsyncToken;
		service.<span style="color: #006600;">resultFormat</span> = <span style="color: #ff0000;">&quot;e4x&quot;</span>;
		service.<span style="color: #0066CC;">url</span> = BASE_URL+<span style="color: #ff0000;">&quot;gallery.xml&quot;</span>;
		token = service.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		token.<span style="color: #006600;">addResponder</span><span style="color: #66cc66;">&#40;</span>responder<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceResult<span style="color: #66cc66;">&#40;</span>event:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = galleryFactory.<span style="color: #006600;">createGallery</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">result</span>.<span style="color: #006600;">image</span>, BASE_URL<span style="color: #66cc66;">&#41;</span>;
		galleryUpdated.<span style="color: #006600;">dispatch</span><span style="color: #66cc66;">&#40;</span> gallery <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceFault<span style="color: #66cc66;">&#40;</span>event:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<h3>IGalleryImageService</h3>

<div class="wp_codebox"><table><tr id="p38622"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p386code22"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGalleryImageService
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> loadGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The service and its factory class are also mapped as singleton interfaces. This makes it really easy to swap out services (to add a Flickr service for example).</p>
<h3>GalleryUpdatedSignal</h3>

<div class="wp_codebox"><table><tr id="p38623"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p386code23"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GalleryUpdatedSignal <span style="color: #0066CC;">extends</span> Signal
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GalleryUpdatedSignal<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>Gallery<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The signals are simple classes that merely extend the Signal class and declare their payload type in the super() of the constructor. Signals are a really marvelous concept and provide a lot of very nice functionality. They make a great companion to Robotlegs and I think you will see some really cool stuff being done with Signals and Robotlegs in the very near future.</p>
<h3>LoadGalleryCommand</h3>

<div class="wp_codebox"><table><tr id="p38624"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p386code24"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoadGalleryCommand
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> service:IGalleryImageService;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
	<span style="color: #66cc66;">&#123;</span>
		service.<span style="color: #006600;">loadGallery</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The application has a single command, LoadGalleryCommand, that is used to actually load the initial images from service and is only called once. In fact, the event that is fired to launch this command in the context startup method is the only event that this application uses (outside of the Flex events on the UI items). That is awesome!</p>
<p>Something you will notice right away looking at the source is that the ImageGalleryContext is the only class that extends a Robotlegs class. You could do without that too, frankly, and create your own context that created the maps and injector instances. I'm not a masochist however, and I will use the provided context for this example. There is a huge amount of potential there for implementing your own custom contexts. I think an interesting implementation might pitch Flash Events altogether and use only Signals for communication between application actors. This example is essentially doing that, but I'd like to get rid of that single event in startup too!</p>
<p><a href="http://github.com/joelhooks/robotlegs-examples-ImageGalleryPM/zipball/v1.0.0"><strong>Download the full source as a Zip</strong></a></p>
<p>Let me know if you make something cool with Robotlegs and post details about it, I've got a stack of these to give away:</p>
<div align="center"><a href="http://69.164.207.135/wp-content/uploads/2010/01/photo.jpg"><img src="http://joelhooks.com/wp-content/uploads/2010/01/photo-225x300.jpg" alt="photo" title="photo" width="225" height="300" /></a></div>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2010/01/16/robotlegs-image-gallery-example-using-as3-signals-and-the-presentation-model/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Robotlegs IoC/DI Flex Framework Examples Updated</title>
		<link>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/</link>
		<comments>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 19:32:51 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=314</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
Live demos are available on robotlegs.org here Acme Widget Factory: The Acme Widget Factory is modular example and deals with loading multiple modules into a shell and communicating between those modules. The shell and each module has its own context. Lazy Stack: Lazy Stack is an example of deferred instantiation of Flex components, and how [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>Live demos are available on <a href="http://www.robotlegs.org/examples/">robotlegs.org here</a></p>
<p>Acme Widget Factory:<br />
The Acme Widget Factory is modular example and deals with loading multiple modules into a shell and communicating between those modules. The shell and each module has its own context.</p>
<p>Lazy Stack:<br />
Lazy Stack is an example of deferred instantiation of Flex components, and how this is handled by the Robotlegs framework.</p>
<p>Wheres Window:<br />
Where's Window is a simple example to show how to mediate and communicate between a set of instances of the same view component that have individual mediators. In this case, the view components are windows in an AIR application. This uses a proxy to register and manage the currently open unique windows. I am going to write a more detailed breakdown of this particular example soon.</p>
<h2>The Elevator Pitch</h2>
<p><a href="http://github.com/darscan/robotlegs/tree/master">Robotlegs</a> is an IoC/DI container that is modeled on the PureMVC application structure. It is designed to be MVCS, model-view-controller-service, providing metadata dependency injection across these layers. While following the well organized approach of PureMVC, it eliminates the use of framework singletons and takes full advantage of the Flash Platform to keep the dreaded boiler plate code to a minimum. Eliminating these singletons makes unit testing and TDD much easier, which can be a real struggle when trying to isolate classes for testing where these singleton dependencies exist. Robotlegs is fast, clean, and perhaps more importantly fun to work with. There is a smorgasbord of architectural frameworks available for Flex/AS3, but Robotlegs is worth a look even if you are sick of looking at architectural frameworks.<br />
<a href="http://github.com/darscan/robotlegs/tree/master"><img class="aligncenter size-full wp-image-262" title="robotlegssketchsmall" src="http://69.164.207.135/wp-content/uploads/2009/07/robotlegssketchsmall.gif" alt="robotlegssketchsmall" width="221" height="260" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/08/14/robotlegs-iocdi-flex-framework-examples-updated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Unit Testing: IoC/DI, Robotlegs and FlexUnit 4</title>
		<link>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/</link>
		<comments>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 06:08:24 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[FlexUnit]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=283</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
I've been reviewing the various IoC containers available for Flex/Actionscript. One of the benefits of IoC and DI is that it greatly facilitates unit testing. By injecting our dependencies into our applications actors, we are effectively isolating them from the other classes that make the application function. "So what?" In computer programming, unit testing is [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>I've been reviewing the various IoC containers available for Flex/Actionscript. One of the benefits of IoC and DI is that it greatly facilitates unit testing. By injecting our dependencies into our applications actors, we are effectively isolating them from the other classes that make the application function. "So what?"</p>
<blockquote><p>In computer programming, unit testing is a software verification and validation method in which a programmer tests that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a class, which may belong to a base/super class, abstract class or derived/child class.</p>
<p>Ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation. -<a href="http://en.wikipedia.org/wiki/Unit_testing">wikipedia unit testing entry</a></p></blockquote>
<p>To effectively unit test classes, we want them to be as isolated as possible. The class needs to stand on its own and have its functionality vetted to ensure that it behaves as expected. When we start to test how our classes behave together, we have entered the land of <a href="http://en.wikipedia.org/wiki/Integration_testing">integration testing</a>. Integration testing is a worthwhile pursuit also, but before we get to that point, we really want to ensure that our classes are solid by themselves.</p>
<p>Using the example in this <a href="http://joelhooks.com/2009/07/17/robotlegs-as3-a-dependency-injection-driven-mvcs-framework-for-flashflex-–-inspired-by-puremvc/">Robotlegs Image Gallery demonstration</a>, we are going to test a couple of the classes that make up the application. The demo isn't complex. It has a very simple model, a single service, and just a few views. In fact, it might even seem like a waste of time to unit test a simple example such as this, but the concepts are applicable to larger applications where the benefits of this type of testing really start to pay off.</p>
<p><a href="http://opensource.adobe.com/wiki/display/flexunit/FlexUnit+4+feature+overview">FlexUnit 4 is the framework</a> that is used here. It provides very handy asynchronous testing, meta data support, and a whole bucket of stellar features that I won't even begin to scratch the surface of. FlexUnit 4 also comes with a graphical testrunner interface:</p>
<p><img class="aligncenter size-full wp-image-284" title="success" src="http://joelhooks.com/wp-content/uploads/2009/07/success.jpg" alt="success" width="450" height="277" /><br />
<span id="more-283"></span></p>
<h2>On to the code...</h2>
<p><a href="http://github.com/joelhooks/robotlegsdemos/tree/master">full source is here...</a></p>
<p>This test runner interface is added to the RobotlegsImageGalleryTestRunner.mxml application:</p>

<div class="wp_codebox"><table><tr id="p28329"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p283code29"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:flexUnitUIRunner</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2009/flexUnitUIRunner&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;onCreationComplete()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:BasicLayout</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">			import org.robotlegs.demos.imagegallery.test.suites.RobotlegsImageGalleryTestSuite;</span>
<span style="color: #339933;">			import org.flexunit.listeners.UIListener;</span>
<span style="color: #339933;">			import org.flexunit.runner.FlexUnitCore;</span>
&nbsp;
<span style="color: #339933;">			private var core:FlexUnitCore;</span>
<span style="color: #339933;">			private function onCreationComplete():void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				this.core = new FlexUnitCore();</span>
<span style="color: #339933;">				core.addListener( new UIListener( uiListener ));</span>
<span style="color: #339933;">				core.run( RobotlegsImageGalleryTestSuite )</span>
<span style="color: #339933;">			}</span>
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flexUnitUIRunner:TestRunnerBase</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;uiListener&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The core runs our test suites, in this case we are only running a single suite: the RobotlegsImageGalleryTestSuite. The test suite carries metadata that identifies it as a FlexUnit suite. There is no need to extend TestSuite as in previous version of FlexUnit:</p>

<div class="wp_codebox"><table><tr id="p28330"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p283code30"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">suites</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>.<span style="color: #006600;">TestFlickrService</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>.<span style="color: #006600;">TestGalleryProxy</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Suite<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>RunWith<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;org.flexunit.runners.Suite&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RobotlegsImageGalleryTestSuite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> testFlickrService:TestFlickrService;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> testGalleryProxy:TestGalleryProxy;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The suite further defines the test cases that we are going to run, TestFlickrService and TestGalleryProxy. Any other test cases would also be included in this suite. TestFlickrService extends the Robotlegs Service class. It makes an asyncronous call to the Flickr API and retrieves images via the 'interestingness' category, as well as through a text search. The Service class has a dependency that is injected by the framework on its eventBroadcaster property. EventBroadcaster simply wraps an event dispatcher to provide a simplified interface for the framework to access. Service also provides a dispatch(event) convenience method for sending event notifications to the framework context. Our tests are not run within this context, however, so we have run into a problem. Since the framework injects the EventDispatcher into the EventBroadcaster automatically, we apparently have no way to access it and we can't receive framework notifications.</p>
<p>As it happens, we can simply inject our dependencies manually. The TestFlickrService case has a serviceDispatcher property that extends EventDispatcher. In our setUp() method that is called prior to every test, we create a new EventBroadcaster (which takes our serviceDispatcher as an argument) and add it to our fresh FlickrImageService instance. Now the test case has access to the injected dispatcher, and we can listen for our normal flash events that Robotlegs uses for notifications.</p>

<div class="wp_codebox"><table><tr id="p28331"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code" id="p283code31"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">Assert</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">async</span>.<span style="color: #006600;">Async</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryEvent</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">remote</span>.<span style="color: #006600;">services</span>.<span style="color: #006600;">FlickrImageService</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">mvcs</span>.<span style="color: #006600;">EventBroadcaster</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestFlickrService
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> service:FlickrImageService;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> serviceDispatcher:EventDispatcher = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Before<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			serviceDispatcher = <span style="color: #000000; font-weight: bold;">new</span> EventDispatcher<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			service = <span style="color: #000000; font-weight: bold;">new</span> FlickrImageService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			service.<span style="color: #006600;">eventBroadcaster</span> = <span style="color: #000000; font-weight: bold;">new</span> EventBroadcaster<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span><span style="color: #66cc66;">&#41;</span>;;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>After<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">eventBroadcaster</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testRetreiveImages<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> GalleryEvent.<span style="color: #006600;">GALLERY_LOADED</span>, Async.<span style="color: #006600;">asyncHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, handleImagesReceived, <span style="color: #cc66cc;">3000</span>, <span style="color: #000000; font-weight: bold;">null</span>, handleServiceTimeout<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">loadGallery</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#40;</span>async<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSearchImages<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">serviceDispatcher</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> GalleryEvent.<span style="color: #006600;">GALLERY_LOADED</span>, Async.<span style="color: #006600;">asyncHandler</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>, handleImagesReceived, <span style="color: #cc66cc;">3000</span>, <span style="color: #000000; font-weight: bold;">null</span>, handleServiceTimeout<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">search</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;robotlegs&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleServiceTimeout<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">object</span>:<span style="color: #0066CC;">Object</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
	    	        Assert.<span style="color: #006600;">fail</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Pending Event Never Occurred'</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleImagesReceived<span style="color: #66cc66;">&#40;</span>event:GalleryEvent, <span style="color: #0066CC;">object</span>:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The gallery should have some photos: &quot;</span>, event.<span style="color: #006600;">gallery</span>.<span style="color: #006600;">photos</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>After each test is run, our tearDown method is called by FlexUnit which sets our service and its eventBroadcaster to null in preparation for the next test in this case to be run. While this example isn't very complex, it is easy to see how this could be extended to a much more complicated service.</p>
<p>The GalleryProxy class is tested in much the same way, though it doesn't have any async methods:</p>

<div class="wp_codebox"><table><tr id="p28332"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
</pre></td><td class="code" id="p283code32"><pre class="actionscript" style="font-family:monospace;">package org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">test</span>.<span style="color: #006600;">cases</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">flexunit</span>.<span style="color: #006600;">Assert</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">proxies</span>.<span style="color: #006600;">GalleryProxy</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">Gallery</span>;
	<span style="color: #0066CC;">import</span> org.<span style="color: #006600;">robotlegs</span>.<span style="color: #006600;">demos</span>.<span style="color: #006600;">imagegallery</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">GalleryImage</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestGalleryProxy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> galleryProxy:GalleryProxy;
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Before<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span> = <span style="color: #000000; font-weight: bold;">new</span> GalleryProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>After<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span> = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSetGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = <span style="color: #000000; font-weight: bold;">new</span> Gallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> = gallery;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;GalleryProxy should have a gallery&quot;</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>Test<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testSetSelectedImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> image1:GalleryImage = <span style="color: #000000; font-weight: bold;">new</span> GalleryImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> image2:GalleryImage = <span style="color: #000000; font-weight: bold;">new</span> GalleryImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = <span style="color: #000000; font-weight: bold;">new</span> Gallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">gallery</span> = gallery;
			gallery.<span style="color: #006600;">photos</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>image1<span style="color: #66cc66;">&#41;</span>;
			gallery.<span style="color: #006600;">photos</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>image2<span style="color: #66cc66;">&#41;</span>;
			image1.<span style="color: #006600;">selected</span> = <span style="color: #000000; font-weight: bold;">false</span>;
			image2.<span style="color: #006600;">selected</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryProxy</span>.<span style="color: #006600;">setSelectedImage</span><span style="color: #66cc66;">&#40;</span>image1<span style="color: #66cc66;">&#41;</span>;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Image1 should be selected&quot;</span>, image1.<span style="color: #006600;">selected</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
			Assert.<span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Image 2 should NOT be selected&quot;</span>, image2.<span style="color: #006600;">selected</span>, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With these simple tests we are 100% certain that our classes function as expected on their own, completely isolated from the rest of the application.</p>
<h2>You really should call your mom...</h2>
<p>We are all unit testing our code and following strong Agile/TDD processes, right.... right? I don't know how much unit testing is going on in the Flash/Flex world, but my guess that the overall test coverage percentage is very small. RIAs are here. They are becoming full fledged applications that <strong><em>people rely on to do business</em></strong>. There will come a time in your RIAs life that you will need to get in there and do some refactoring. It might be for performance, fixing bugs, or adding some new kickass feature to push your app to the next level. Fact, without unit testing:</p>
<blockquote><p><strong>you're not refactoring; you're just changing shit. -<a href="http://hamletdarcy.blogspot.com/2009/06/forgotten-refactorings.html">Hamlet D'Arcy</a></strong></p></blockquote>
<p>There are many, many arguments for unit testing your code, but for me this is the most compelling. At some point in the future, you are going to want to get in there and make some changes. If your application is important to your users, you want to make sure those changes don't break everything. It is guaranteed that they will break something, the question is will you know immediately what happened, or at least where to look?</p>
<p>We are getting the proper tools for the job. FlexUnit 4 is an incredible project. There is a robust selection of frameworks that all have merits and can facilitate structured, well designed applications. I'm personally out of excuses. I want to develop applications that my users can trust and rely on to do business, and unit testing is a part of that recipe.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Inversion of Control and Dependency Injection with Flex using the Parsley Application Framework &#8211; Part 2</title>
		<link>http://joelhooks.com/2009/07/14/inversion-of-control-and-dependency-injection-with-flex-using-the-parsley-application-framework-part-2/</link>
		<comments>http://joelhooks.com/2009/07/14/inversion-of-control-and-dependency-injection-with-flex-using-the-parsley-application-framework-part-2/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 14:31:56 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[design patterns]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[object oriented concepts]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=226</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
This is the second part in a series exploring Inversion of Control and Dependency Injection containers available for Flex. In the first part of the series we discussed what IoC and DI are and how they can benefit our applications. In this part, we will explore the Parsley IoC Container through a simple image gallery [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>This is the second part in a series exploring Inversion of Control and Dependency Injection containers available for Flex. In <a href="http://joelhooks.com/2009/07/12/inversion-of-control-and-dependency-injection-in-flex-using-the-parsley-application-framework-part-1/">the first part of the series we discussed what IoC and DI</a> are and how they can benefit our applications. In this part, we will explore the Parsley IoC Container through a simple image gallery application.</p>
<p><a href="http://github.com/joelhooks/IoC-DI-Flex-Example/tree/master">Here's the source on github...</a></p>
<div align="center">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_ParsleyGallery_1559489805"
			class="flashmovie"
			width="500"
			height="600">
	<param name="movie" value="http://joelhooks.com/examples/parsleyGallery/ParsleyGallery.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://joelhooks.com/examples/parsleyGallery/ParsleyGallery.swf"
			name="fm_ParsleyGallery_1559489805"
			width="500"
			height="600">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<div class="note-block">The above is a simple image gallery that is grabbing the top 100 'interestingness' images from Flickr. It is built with Flex 4 and uses a mix of halo components (the images) and spark components for layout. Parsley in its current state does NOT support the Flex 4.0.0 SDK. It IS however opensource, and only required a single change to function in Flex 4. Namely, there is a call to Application.application which has been 'deprecated' (and by that they mean completely broken) and replaced by FlexGlobals.topLevelApplication. The modified SWC files are included in the source repository for the project. <strong>The modified swc files WILL NOT function with Flex 3.*.*</strong></div>
<p><span id="more-226"></span></p>
<h2>Less talk. More Code.</h2>
<p>One aspect of an IoC/DI container that is consistent across frameworks is the need to bootstrap the configuration. Parsley provides several ways to accomplish this.</p>
<p><a href="http://www.spicefactory.org/parsley/docs/2.0/manual/config.php#intro">from the docs</a>:</p>
<blockquote><p>Configuring and initializing the Parsley Framework usually consists of the following steps:</p>
<p><strong>Step 1</strong>: Telling the IOC Container which classes it should manage. This can be done with MXML, XML files or in ActionScript. All three mechanisms will be described in the following sections.</p>
<p><strong>Step 2:</strong> Configure the container services like Dependency Injection or Messaging for each individual class. This can be done with the mechanism you had chosen for step 1 (e.g. with MXML or XML configuration tags) or - in most cases - conveniently with AS3 Metadata Tags right within the classes themselves.</p>
<p><strong>Step 3:</strong> Initialize the IOC Container (usually on application startup). In Parsley 2 this is a one-liner in most cases. See the sections below for examples.</p></blockquote>
<p>In the image gallery sample step one is accomplished in Beans.mxml. Beans is, I assume, a throwback to Java that I don't fully understand, but it seems to be a popular convention and I'm not here to rock the boat.</p>
<h3>Beans.mxml</h3>

<div class="wp_codebox"><table><tr id="p22638"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p226code38"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">	Inversion of Control/Dependency Injection Using Parsley</span>
<span style="color: #808080; font-style: italic;">	Image Gallery</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Any portion of this demonstration may be reused for any purpose where not</span>
<span style="color: #808080; font-style: italic;">	licensed by another party restricting such use. Please leave the credits intact.</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Joel Hooks</span>
<span style="color: #808080; font-style: italic;">	http://joelhooks.com</span>
<span style="color: #808080; font-style: italic;">	joelhooks@gmail.com</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Object</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.spicefactory.org/parsley&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:models</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.models.*&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:controllers</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.controllers.*&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">xmlns:presentation</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.models.presentation.*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    	<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">    		import com.joelhooks.parsley.example.delegates.FlickrImageServiceDelegate;</span>
<span style="color: #339933;">    	]]&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Service Delegate --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;photoServiceDelegate&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;{FlickrImageServiceDelegate}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;apiKey&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;YOUR_KEY_HERE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;secret&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;YOUR_SECRET_HERE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Model --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;presentation:FlickrGalleryPresentationModel</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;galleryPresentationModel&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- Actions --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controllers:RetrieveGalleryAction</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controllers:DisplayGalleryAction</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;controllers:SelectImageAction</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mx:Object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>The application has several actors that Parsley needs to be aware of, and this is where you let the container know what they are. In most cases we can simple use MXML notation to include our classes in the configuration. The photo service, in this case Flickr, needs additional configuration so it is added a bit differently utilizing the Parsley Object and Property tags. When using this method the class needs to be imported as above to provide a reference. In addition to the service the presentation model is also added to the configuration, as well as several actions that will react to various events. The Model and Controller of the MVC triad are what we are configuring here. The View is handled a bit differently.</p>
<p>The configuration is loaded in the preinitialize handle of the main application MXML.</p>
<h3>ParsleyGallery.mxml:</h3>

<div class="wp_codebox"><table><tr id="p22639"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code" id="p226code39"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">	Inversion of Control/Dependency Injection Using Parsley</span>
<span style="color: #808080; font-style: italic;">	Image Gallery</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Any portion of this demonstration may be reused for any purpose where not</span>
<span style="color: #808080; font-style: italic;">	licensed by another party restricting such use. Please leave the credits intact.</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Joel Hooks</span>
<span style="color: #808080; font-style: italic;">	http://joelhooks.com</span>
<span style="color: #808080; font-style: italic;">	joelhooks@gmail.com</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Application</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">preinitialize</span>=<span style="color: #ff0000;">&quot;onPreInitialize()&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">minWidth</span>=<span style="color: #ff0000;">&quot;1024&quot;</span> <span style="color: #000066;">minHeight</span>=<span style="color: #ff0000;">&quot;768&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:tag</span>=<span style="color: #ff0000;">&quot;org.spicefactory.parsley.flex.tag.*&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:spicefactory</span>=<span style="color: #ff0000;">&quot;http://www.spicefactory.org/parsley/flex&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">addedToStage</span>=<span style="color: #ff0000;">&quot;this.addedToStageHandler(event)&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">applicationComplete</span>=<span style="color: #ff0000;">&quot;trace('application complete');&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:views</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.views.*&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:delegates</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.delegates.*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:VerticalLayout</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">			import com.joelhooks.parsley.example.delegates.FlickrImageServiceDelegate;</span>
<span style="color: #339933;">			import mx.controls.Button;</span>
<span style="color: #339933;">			import org.spicefactory.lib.reflect.types.Void;</span>
<span style="color: #339933;">			import org.spicefactory.lib.logging.Logger;</span>
<span style="color: #339933;">			import mx.logging.LogEventLevel;</span>
<span style="color: #339933;">			import com.joelhooks.parsley.example.events.GalleryImageServiceEvent;</span>
<span style="color: #339933;">			import org.spicefactory.parsley.flex.FlexContextBuilder;</span>
&nbsp;
<span style="color: #339933;">			protected function onPreInitialize():void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				this.logTarget.level = LogEventLevel.ALL;</span>
<span style="color: #339933;">				FlexContextBuilder.build(Beans);</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			protected function addedToStageHandler(event:Event):void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				//Let the container know that this component is ready to be added</span>
<span style="color: #339933;">				//to the view context</span>
<span style="color: #339933;">				dispatchEvent(new Event('configureIOC', true));</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:TraceTarget</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;logTarget&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Declarations<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;views:GalleryView</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>We want to make sure that the Parsley container is configured at the first opportunity, and preinitialize provides this hook. After the application has been added to the stage, we need to dispatch a message to the container so that it can be added to the ViewContext allowing the visual components to interact with the container. This is done by dispatching a bubbling 'configureIOC' event in the addedtoStage handler.</p>
<p>This needs to be done for all viewComponents that require access to the container as you will see in the child GalleryView. At this point, the application is fully configured within the IoC Container. Our actors that have been configured in Beans.mxml have had their appropriate dependencies injected, and our Actions are ready to receive messages from the other objects in the container.</p>
<h3>DisplayGalleryAction.as:</h3>

<div class="wp_codebox"><table><tr id="p22640"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code" id="p226code40"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
	Inversion of Control/Dependency Injection Using Parsley
	Image Gallery
&nbsp;
	Any portion of this demonstration may be reused for any purpose where not
	licensed by another party restricting such use. Please leave the credits intact.
&nbsp;
	Joel Hooks
	http://joelhooks.com
	joelhooks@gmail.com
*/</span>
package com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">controllers</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryEvent</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryImageEvent</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">presentation</span>.<span style="color: #006600;">IGalleryPresentationModel</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">GalleryImage</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;selectGalleryImage&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.events.GalleryImageEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>ManagedEvents<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;selectGalleryImage&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DisplayGalleryAction <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Inject<span style="color: #66cc66;">&#40;</span>id=<span style="color: #ff0000;">&quot;galleryPresentationModel&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> galleryPresentationModel:IGalleryPresentationModel;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DisplayGalleryAction<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>MessageHandler<span style="color: #66cc66;">&#40;</span>selector=<span style="color: #ff0000;">&quot;galleryLoaded&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleGalleryLoaded<span style="color: #66cc66;">&#40;</span>event:GalleryEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">galleryPresentationModel</span>.<span style="color: #006600;">gallery</span> = event.<span style="color: #006600;">gallery</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">gallery</span>.<span style="color: #006600;">photos</span>.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GalleryImageEvent<span style="color: #66cc66;">&#40;</span>GalleryImageEvent.<span style="color: #006600;">SELECT_GALLERY_IMAGE</span>,
				event.<span style="color: #006600;">gallery</span>.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> as GalleryImage<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The DisplayGalleryAction class is wired to listen for the system message (event) galleryLoaded. It has no interest in where this message originates, and simply responds as instructed when the message is received. The handleGalleryLoaded method is designated as the MessageHandler for the galleryLoaded message. The message is an actionscript event that has been intercepted by the container and routed to interested parties. You will notice that the handleGalleryLoaded method is constructed exactly like any other Actionscript event handler (with the addition of the Parsley metadata). In this case it receives the custom GalleryEvent that carries some information.</p>
<p>Additionally, DisplayGalleryAction has been injected with our FlickrGalleryPresentationModel from Beans.mxml. FlickrGalleryPresentationModel implements IGalleryPresentationModel, and Parsley will inject any class that implements this interface and has the appropriate ID 'galleryPresentationModel'.</p>
<p>DisplayGalleryAction also dispatches the ManagedEvent 'selectGalleryImage'. Any container actor that dispatches events that need to be distributed to other interested parties need to let the container know. The class is annotated with AS3 metadata designating which ManagedEvent the class will be sending to the container.</p>
<div class="note-block">It is important to note that Parsley does not impose any strict patterns on your application. Any of the actors can be wired to received dependency injections and send/receive messages from the container. It is up to you to architect your application in the manner most appropriate to your environment.</div>
<p>DisplayGalleryAction receives Messages from FlickrImageServiceDelegate. it could, of course, receive its Messages from any class that is managed by the container, but in this case it is only interested in the data loaded by the delegate.</p>
<h3>FlickrImageServiceDelegate.as:</h3>

<div class="wp_codebox"><table><tr id="p22641"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
</pre></td><td class="code" id="p226code41"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*
	Inversion of Control/Dependency Injection Using Parsley
	Image Gallery
&nbsp;
	Any portion of this demonstration may be reused for any purpose where not
	licensed by another party restricting such use. Please leave the credits intact.
&nbsp;
	Joel Hooks
	http://joelhooks.com
	joelhooks@gmail.com
*/</span>
package com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">delegates</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">webapis</span>.<span style="color: #006600;">flickr</span>.<span style="color: #006600;">FlickrService</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">webapis</span>.<span style="color: #006600;">flickr</span>.<span style="color: #006600;">Photo</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">webapis</span>.<span style="color: #006600;">flickr</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">FlickrResultEvent</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">webapis</span>.<span style="color: #006600;">flickr</span>.<span style="color: #006600;">methodgroups</span>.<span style="color: #006600;">Photos</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">adobe</span>.<span style="color: #006600;">webapis</span>.<span style="color: #006600;">flickr</span>.<span style="color: #006600;">methodgroups</span>.<span style="color: #006600;">helpers</span>.<span style="color: #006600;">PhotoSearchParams</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryEvent</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">events</span>.<span style="color: #006600;">GalleryImageServiceEvent</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">Gallery</span>;
	<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">joelhooks</span>.<span style="color: #006600;">parsley</span>.<span style="color: #006600;">example</span>.<span style="color: #006600;">models</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">GalleryImage</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;serviceReady&quot;</span>, <span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.events.GalleryImageServiceEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>Event<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span>=<span style="color: #ff0000;">&quot;galleryLoaded&quot;</span>,<span style="color: #0066CC;">type</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.events.GalleryEvent&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#91;</span>ManagedEvents<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;galleryLoaded,serviceReady&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FlickrImageServiceDelegate <span style="color: #0066CC;">extends</span> EventDispatcher <span style="color: #0066CC;">implements</span> IGalleryImageServiceDelegate
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> service:FlickrService;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> photos:Photos;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> apiKey:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> secret:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> NSID:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FlickrImageServiceDelegate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#91;</span>PostConstruct<span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Configuring Service&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span> = <span style="color: #000000; font-weight: bold;">new</span> FlickrService<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">apiKey</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">photos</span> = <span style="color: #000000; font-weight: bold;">new</span> Photos<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">service</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GalleryImageServiceEvent<span style="color: #66cc66;">&#40;</span>GalleryImageServiceEvent.<span style="color: #006600;">SERVICE_READY</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadGallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			service.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>FlickrResultEvent.<span style="color: #006600;">INTERESTINGNESS_GET_LIST</span>, handleSearchResult<span style="color: #66cc66;">&#41;</span>;
			service.<span style="color: #006600;">interestingness</span>.<span style="color: #006600;">getList</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> handleSearchResult<span style="color: #66cc66;">&#40;</span>event:FlickrResultEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">processFlickrPhotoResults</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">photos</span>.<span style="color: #006600;">photos</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> processFlickrPhotoResults<span style="color: #66cc66;">&#40;</span>results:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> gallery:Gallery = <span style="color: #000000; font-weight: bold;">new</span> Gallery<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			gallery.<span style="color: #006600;">photos</span> = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> flickrPhoto:Photo <span style="color: #b1b100;">in</span> results<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">var</span> photo:GalleryImage = <span style="color: #000000; font-weight: bold;">new</span> GalleryImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">var</span> baseURL:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'http://farm'</span> + flickrPhoto.<span style="color: #006600;">farmId</span> + <span style="color: #ff0000;">'.static.flickr.com/'</span> + flickrPhoto.<span style="color: #006600;">server</span> + <span style="color: #ff0000;">'/'</span> + flickrPhoto.<span style="color: #006600;">id</span> + <span style="color: #ff0000;">'_'</span> + flickrPhoto.<span style="color: #006600;">secret</span>;
				photo.<span style="color: #006600;">thumbURL</span> = baseURL + <span style="color: #ff0000;">'_s.jpg'</span>;
				photo.<span style="color: #0066CC;">URL</span> = baseURL + <span style="color: #ff0000;">'.jpg'</span>;
				gallery.<span style="color: #006600;">photos</span>.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> photo <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dispatchEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GalleryEvent<span style="color: #66cc66;">&#40;</span>GalleryEvent.<span style="color: #006600;">GALLERY_LOADED</span>, gallery<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As we've seen, the FlickrImageServiceDelegate is an implementation of the IGalleryImageServiceDelegate interface. This allows us to program to an interface instead of an implementation for that loose coupling goodness we all crave. This will come in very handy when we want to modify the application to handle multiple services or build mock objects for the purpose of unit testing (the subject of part 3).</p>
<p>As with the DisplayGalleryAction class, FlickrImageServiceDelegate is configured to send ManagedEvent messages to the container. One of these events is 'serviceReady'. FlickrImageServiceDelegate utilizes the [PostConstruct] annotation on its init method. When this class was configured, we injected properties (the apiKey and secret), and the service cannot be configured without these. Using [PostConstruct] the container is ordered to run the annotated method after the class has been configured thus ensuring our properties have been injected as expected. FlickrImageServiceDelegate then dispatches the 'serviceReady' Message so that the actors waiting to get to work can do what they need to do.</p>
<p>The GalleryView is responsible for displaying the data and accepting user interaction.</p>
<h3>GalleryView.mxml</h3>

<div class="wp_codebox"><table><tr id="p22642"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
</pre></td><td class="code" id="p226code42"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">	Inversion of Control/Dependency Injection Using Parsley</span>
<span style="color: #808080; font-style: italic;">	Image Gallery</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Any portion of this demonstration may be reused for any purpose where not</span>
<span style="color: #808080; font-style: italic;">	licensed by another party restricting such use. Please leave the credits intact.</span>
&nbsp;
<span style="color: #808080; font-style: italic;">	Joel Hooks</span>
<span style="color: #808080; font-style: italic;">	http://joelhooks.com</span>
<span style="color: #808080; font-style: italic;">	joelhooks@gmail.com</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Group</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:fx</span>=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:s</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">xmlns:mx</span>=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/halo&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;500&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;600&quot;</span></span>
<span style="color: #009900;">	<span style="color: #000066;">addedToStage</span>=<span style="color: #ff0000;">&quot;dispatchEvent(new Event('configureIOC', true));&quot;</span> <span style="color: #000066;">creationComplete</span>=<span style="color: #ff0000;">&quot;creationCompleteHandler(event)&quot;</span> <span style="color: #000066;">xmlns:display</span>=<span style="color: #ff0000;">&quot;com.joelhooks.display.*&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:BasicLayout</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		[Event(name=&quot;selectGalleryImage&quot;, type=&quot;com.joelhooks.parsley.example.events.GalleryImageEvent&quot;)]
		[ManagedEvents(&quot;selectGalleryImage&quot;)]
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Metadata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #339933;">&lt;![CDATA[</span>
<span style="color: #339933;">			import org.spicefactory.lib.reflect.types.Void;</span>
<span style="color: #339933;">			import mx.events.FlexEvent;</span>
<span style="color: #339933;">			import com.joelhooks.parsley.example.events.GalleryImageEvent;</span>
<span style="color: #339933;">			import com.joelhooks.parsley.example.models.presentation.IGalleryPresentationModel;</span>
<span style="color: #339933;">			import com.joelhooks.parsley.example.views.renderers.GalleryImageThumbnailItemRenderer;</span>
<span style="color: #339933;">			import mx.collections.ArrayCollection;</span>
&nbsp;
<span style="color: #339933;">			[Bindable]</span>
<span style="color: #339933;">			[Inject(id=&quot;galleryPresentationModel&quot;)]</span>
<span style="color: #339933;">			public var model:IGalleryPresentationModel;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">			protected function creationCompleteHandler(event:FlexEvent):void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				this.dgThumbnails.addEventListener( GalleryImageEvent.SELECT_GALLERY_IMAGE, handleSelectImage);</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">			protected function handleSelectImage(event:GalleryImageEvent):void</span>
<span style="color: #339933;">			{</span>
<span style="color: #339933;">				this.dispatchEvent(event);</span>
<span style="color: #339933;">			}</span>
&nbsp;
<span style="color: #339933;">		]]&gt;</span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fx:Script<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Group</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Rect</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:fill<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mx:SolidColor</span> <span style="color: #000066;">color</span>=<span style="color: #ff0000;">&quot;#000000&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:fill<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Rect<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:Group</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span>  <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:VerticalLayout</span> <span style="color: #000066;">gap</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;display:ImageSwap</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;image&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">source</span>=<span style="color: #ff0000;">&quot;{this.model.selectedImage.URL}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:DataGroup</span></span>
<span style="color: #009900;">			<span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dgThumbnails&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">clipAndEnableScrolling</span>=<span style="color: #ff0000;">&quot;true&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;500&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;85&quot;</span> <span style="color: #000066;">mouseEnabled</span>=<span style="color: #ff0000;">&quot;{!image.changingImages}&quot;</span> <span style="color: #000066;">mouseChildren</span>=<span style="color: #ff0000;">&quot;{!image.changingImages}&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">dataProvider</span>=<span style="color: #ff0000;">&quot;{this.model.gallery.photos}&quot;</span></span>
<span style="color: #009900;">			<span style="color: #000066;">itemRenderer</span>=<span style="color: #ff0000;">&quot;com.joelhooks.parsley.example.views.renderers.GalleryImageThumbnailItemRenderer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:HorizontalLayout</span> <span style="color: #000066;">gap</span>=<span style="color: #ff0000;">&quot;0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:DataGroup<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:HScrollBar</span> <span style="color: #000066;">viewport</span>=<span style="color: #ff0000;">&quot;{this.dgThumbnails}&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;500&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/s:Group<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As with the main application MXML, the GalleryView sends a 'configureIOC' event to the container when it is added to the stage. This allows it to participate in the Messaging system and have its dependencies injected. GalleryView dispatches one message, 'selectGalleryImage', when a user clicks a thumbnail. It is also injected with a reference to the applications IGalleryPresentationModel (FlickrGalleryPresentationModel) where it accesses the array collection containing the objects presented in the thumbnail DataGroup.</p>
<p>There are two more Actions, RetreiveGalleryAction and SelectImageAction that function as their names imply. In addition the project has several custom events that are used for messaging. I'm not going to detail these as it would be repetitive, but it is worth noting that this only scratches the surface of the features that are available in Parsley. The docs are extensive, and the API is extremely robust. Typically you won't ever need to access the API directly after you make the call in the preinitialize method to bootstrap the container. After that it does its work silently in the background.</p>
<h2>So I think...</h2>
<p>Parsley could be considered a 'magical' black box framework. This is the trade off you get removing all of the boiler plate code you see in other frameworks. Looking at the extent of classes and libraries that make up Parsley is intimidating. You are placing a lot of trust in a complicated framework and have to hope that trust isn't misplaced. There is something to be said for a simple 'non-magical' framework like PureMVC. Yes, it comes at the cost of boiler plate code, but comprehension is less of a concern.</p>
<p>The biggest challenge Parsley presents to learning the framework is that code examples are scarce (at best) outside of the out of context examples in the (excellent otherwise) documentation and a <a href="http://www.sitronnier.com/blog/parsley-2-basic-flex-example">couple blog posts</a>. Researching the framework, I actually found myself referring to SWiZ examples to understand some of the applied concepts. This is interesting in that the SWiZ examples really do carry over, as the frameworks have significant overlap in scope and functionality. There is <a href="http://groups.google.com/group/swiz-framework/browse_thread/thread/dc418a286c9a3f76">a thread on the SWiZ Google Group</a> that goes into this, and I was surprised to see the complete lack of knowledge even to the existence of Parsley. I've been aware of the framework since early 2008, but didn't take time to get my head around it. It really is perplexing as to why there aren't more examples floating around, as Parsley is very mature and robust at this stage in its development. That is something that I really love about PureMVC. Cliff has done such an excellent job centralizing the examples and framework tutorials with <a href="http://trac.puremvc.org/PureMVC_AS3/">the manifold project</a>. The effort it takes to maintain are huge, so I understand that most don't have the time or inclination to do so, but it is much appreciated to be sure.</p>
<p>In Part 3 we will go into swapping out the service and get a little unit testing coverage on the application.</p>
<div class="note-block">I got a little distracted with <a href="http://joelhooks.com/2009/07/17/robotlegs-as3-a-dependency-injection-driven-mvcs-framework-for-flashflex-–-inspired-by-puremvc/">Robotlegs</a> and wrote <a href="http://joelhooks.com/2009/07/21/unit-testing-with-inversion-of-control-ioc-and-dependency-injection-di-with-the-robotlegs-framework-and-flexunit-4/">part 3</a> of this tutorial with that framework instead. Parsley is awesome, but Robotlegs suits my needs and makes me smile.</div>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/07/14/inversion-of-control-and-dependency-injection-with-flex-using-the-parsley-application-framework-part-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Inversion of Control and Dependency Injection in Flex using the Parsley Application Framework &#8211; Part 1</title>
		<link>http://joelhooks.com/2009/07/12/inversion-of-control-and-dependency-injection-in-flex-using-the-parsley-application-framework-part-1/</link>
		<comments>http://joelhooks.com/2009/07/12/inversion-of-control-and-dependency-injection-in-flex-using-the-parsley-application-framework-part-1/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 00:53:34 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[design patterns]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=194</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
The Inversion of Control (IoC) design principle and the related Dependency Injection (DI) design pattern have been around for several years. In the Java world, popular frameworks such as Spring make heavy use of these concepts. With the introduction of Actionscript 3 in 2006, the opportunities for framework development increased greatly. As the community matures, [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p>The Inversion of Control (IoC) design principle and the related Dependency Injection (DI) design pattern have been around for several years. In the Java world, popular frameworks such as Spring make heavy use of these concepts. With the introduction of Actionscript 3 in 2006, the opportunities for framework development increased greatly. As the community matures, we are starting to see robust tools appear on the development landscape. It is a good time to be a Flash geek!</p>
<p>Through this series of articles, we will delve into the <a href="http://www.spicefactory.org/parsley/">Parsley application framework</a> and look at how it implements IoC and DI in the context of a photo slideshow. Through this example we will look at bootstrapping the container, connecting to services, and populating our components with the results. In this first part, we will get an overview of IoC and DI and talk about some of the benefits and drawbacks of using these concepts in our applications.<br />
<span id="more-194"></span></p>
<h3>What is Inversion of Control?</h3>
<blockquote><p>One important characteristic of a framework is that the methods defined by the user to tailor the framework will often be called from within the framework itself, rather than from the user's application code. The framework often plays the role of the main program in coordinating and sequencing application activity. This inversion of control gives frameworks the power to serve as extensible skeletons. The methods supplied by the user tailor the generic algorithms defined in the framework for a particular application. --Ralph Johnson and Brian Foote [<a href="http://www.laputan.org/drc/drc.html">1</a>]</p></blockquote>
<p>If you are using an Actionscript framework in your development, odds are you’re making use of IoC. This includes <a href="http://www.adobe.com/products/flex/">Flex</a> itself, <a href="http://puremvc.org/">PureMVC</a>, <a href="http://mate.asfusion.com/">Mate</a>, <a href="http://code.google.com/p/swizframework/">SWiZ</a>, Parsley, and other similar frameworks. The framework is providing an abstracted skeleton to which the developer can code against. Communication between the actors in the system are coordinated by the framework within a predefined architecture. In other words, some level of control is inverted from the individual components of an application to the underlying framework.</p>
<p>This inversion can be approached from many different directions. In frameworks such as PureMVC and <a href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm">Cairngorm</a>, this inversion of control is handled by extension of framework classes. By extending these objects, the framework is provided with the appropriate interface to access and manipulate the application specific objects. Frameworks such as Parsley and SWiZ provide metadata annotation as the mechanism for wiring into the framework.</p>
<h3>What is Dependency Injection?</h3>
<p>DI is a design pattern canonized by Martin Fowler [<a href="http://martinfowler.com/articles/injection.html">2</a>]. Essentially, your application’s objects do not configure themselves. They rely on an external mechanism for configuration that ‘injects’ the object’s dependencies by populating its instance variables. Fowler felt that Inversion of Control was too generic of a description (as all frameworks tend to invert control), and decided on the name Dependency Injection for the pattern.</p>
<blockquote><p>Dependency Injection means giving an object its instance variables [<a href="http://jamesshore.com/Blog/Dependency-Injection-Demystified.html">3</a>]</p></blockquote>
<p>Say what? Given this simple definition of DI, this is something we are all making use of on a constant basis. Unless you are writing strictly procedural AS3 (which is certainly possible), DI is likely being used in your applications. Yes, the concept can quickly become more complex - abstracting simple concepts tends to have that effect.</p>
<p>The pattern describes three primary methods for injecting our dependencies:</p>
<div>
<p>
<strong>Constructor Injection:</strong></p>

<div class="wp_codebox"><table><tr id="p19449"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p194code49"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dependsOn:IDependency;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span>dependsOn:IDependency<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dependsOn</span> = dependsOn;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>usage:</p>

<div class="wp_codebox"><table><tr id="p19450"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p194code50"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myObject:MyObject = <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span>aDependencyImplementation<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

</p>
</div>
<div>
<p>
<strong>Setter Injection:</strong></p>

<div class="wp_codebox"><table><tr id="p19451"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p194code51"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dependsOn:IDependency;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDependsOn<span style="color: #66cc66;">&#40;</span>value:IDependency<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dependsOn</span> = value;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>usage:</p>

<div class="wp_codebox"><table><tr id="p19452"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p194code52"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myObject:MyObject = <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
myObject.<span style="color: #006600;">setDependsOn</span><span style="color: #66cc66;">&#40;</span>aDependencyImplementation<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

</p>
</div>
<div>
<p>
<strong>Property Injection:</strong></p>

<div class="wp_codebox"><table><tr id="p19453"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p194code53"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyObject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> dependsOn:IDependency;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>usage:</p>

<div class="wp_codebox"><table><tr id="p19454"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p194code54"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> myObject:MyObject = <span style="color: #000000; font-weight: bold;">new</span> MyObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
myObject.<span style="color: #006600;">dependsOn</span> =  aDependencyImplementation;</pre></td></tr></table></div>

</p>
</div>
<p>Setter and property injection are both very similar, and are useful for optional dependencies. Constructor injection is useful for required dependencies that an object must have to function.</p>
<h3>Benefits of Dependency Injection</h3>
<p>One of the primary benefits of DI is related to unit testing your application. DI allows you to create loosely couple objects that can be easily replaced with mock or stub objects for testing. Unit testing needs to test your objects in isolation, without regard for how the object interacts with its various dependencies.</p>
<blockquote><p>A unit test is a user of the object it tests. An object must exist for a reason, so there must be other objects that also use it. Even when only one application object uses it, the fact that the unit test is also a user means it has at least two users, and therefore the object must be reusable by design. Test-driven development (TDD), in which unit tests are written before the objects they test, is gaining as a development best practice. One beneficial consequence of TDD is that objects are designed with testability in mind, and therefore, an object and its dependencies must be loosely coupled and reusable. Dependency injection is a popular pattern to design loosely coupled—and therefore robust, reusable, and testable—objects. [<a href="http://www.ddj.com/development-tools/185300375">4</a>]</p></blockquote>
<p>DI can also encourage component reuse in much the same way. Since the object’s dependencies are not hard-coded, it is open for reuse. The injectable objects are more adaptable allowing developers to see clearly where to start adding features. The loose coupling can create a more maintainable code-base allowing developers to more easily track down bugs.</p>
<h3>Drawbacks of Dependency Injection</h3>
<blockquote><p>No Silver Bullets. [<a href="http://www.virtualschool.edu/mon/SoftwareEngineering/BrooksNoSilverBullet.html">5</a>]</p></blockquote>
<p>DI can potentially obfuscate an application. To understand the application a developer has to understand both the code and the configuration defining the dependency injections. In many situations, an IDE will not be able to read and give feedback on the configuration.</p>
<h3>Inversion of Control Containers</h3>
<p>You don’t need an IoC Container (framework) to benefit from DI. As we have seen, you are probably already using DI in your code, irrespective of your preferred framework. IoC Containers provide out of the box methods for instantiating and configuring your application’s objects. The use of an IoC container is dependent on your (and your application’s) needs. For smaller projects, it might not be appropriate.</p>
<p>In <a href="http://joelhooks.com/2009/07/14/inversion-of-control-and-dependency-injection-with-flex-using-the-parsley-application-framework-part-2/">the second part of this series</a>, we will look into the Parsley application framework and its implementation of an IoC container and DI.</p>
<p>[1] <a href="http://www.laputan.org/drc/drc.html">http://www.laputan.org/drc/drc.html</a><br />
[2] <a href="http://martinfowler.com/articles/injection.html">http://martinfowler.com/articles/injection.html</a><br />
[3] <a href="http://jamesshore.com/Blog/Dependency-Injection-Demystified.html">http://jamesshore.com/Blog/Dependency-Injection-Demystified.html</a><br />
[4] <a href="http://www.ddj.com/development-tools/185300375">http://www.ddj.com/development-tools/185300375</a><br />
[5] <a href="http://www.virtualschool.edu/mon/SoftwareEngineering/BrooksNoSilverBullet.html">http://www.virtualschool.edu/mon/SoftwareEngineering/BrooksNoSilverBullet.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/07/12/inversion-of-control-and-dependency-injection-in-flex-using-the-parsley-application-framework-part-1/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>So what&#039;s up with PureMVC Commands anyway?</title>
		<link>http://joelhooks.com/2009/02/08/so-whats-up-with-puremvc-commands-anyway/</link>
		<comments>http://joelhooks.com/2009/02/08/so-whats-up-with-puremvc-commands-anyway/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 02:37:35 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[puremvc]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=146</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
That is what Jesse Warden (JesterXL) asks the fine folks over at the PureMVC forums. Cliff gives a good rundown on the why/when/how of PureMVC Commands. PureMVC's relatively light use of commands, when compared to other widely used MVC frameworks, tends to make one think they are doing something wrong. This isn't really the case. [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://forums.puremvc.org/index.php?topic=993.0">That is what Jesse Warden (JesterXL) asks the fine folks over at the PureMVC forums</a>. Cliff gives a good rundown on the why/when/how of PureMVC Commands.</p>
<p>PureMVC's relatively light use of commands, when compared to other widely used MVC frameworks, tends to make one think they are doing something wrong. This isn't really the case. Commands are still viable actors in our applications, but many times the shortest route is the best route. PureMVC doesn't force us to use them, but gives us access when we need to.</p>
<p>I generally use PureMVC Commands for actions that can be initiated from several places, as well as for application startup. I've also found them very useful for popups and AIR window management.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2009/02/08/so-whats-up-with-puremvc-commands-anyway/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Fabrication &#8211; Simplified PureMVC multicore modules (and more)</title>
		<link>http://joelhooks.com/2008/09/16/fabrication-simplified-puremvc-multicore-modules-and-more/</link>
		<comments>http://joelhooks.com/2008/09/16/fabrication-simplified-puremvc-multicore-modules-and-more/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 15:33:04 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[components]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[puremvc]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=85</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
One of the many reasons I love PureMVC is the community of highly intelligent, skilled, object-oriented (oriented) programmers that are constantly using the framework to build new and interesting utilities. This is a by-product of the very nature of PureMVC. Cliff architected the core framework in such a way that is is truly simple, providing [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p><a href="http://puremvc.org"><img class="aligncenter size-full wp-image-86" title="logo" src="http://69.164.207.135/wp-content/uploads/2008/09/logo.png" alt="" width="481" height="60" /></a></p>
<p>One of the many reasons I love <a href="http://www.puremvc.org/">PureMVC</a> is the community of highly intelligent, skilled, object-oriented (oriented) programmers that are constantly using the framework to build new and interesting utilities. This is a by-product of the very nature of PureMVC. <a href="http://www.linkedin.com/in/cliff">Cliff</a> architected the core framework in such a way that is is truly simple, providing a base from which to build new useful structures.</p>
<p><a href="http://forums.puremvc.org/index.php?topic=681.0">Fabrication, a new utility developed by Darshan Sawardekar</a> is a shining example of this fact. He has taken the <a href="http://puremvc.org/component/option,com_wrapper/Itemid,161/">PureMVC multi-core apperatus</a>, combined it with Cliff's <a href="http://puremvc.org/component/option,com_wrapper/Itemid,170/">Pipes utility</a>, and created a wonderfully abstracted means for creating modular PureMVC applications. Not only does this simplify the connection of modules in your application and allow them to communicate seamlessly, but he provides a router/firewall metaphor for controlling the communication as well as undo/redo functionality.</p>
<p>Thanks Darshan, your efforts are appreciated. And as always, thanks Cliff for providing the base from which our community can build and contribute. Cheers.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/09/16/fabrication-simplified-puremvc-multicore-modules-and-more/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Refactoring: Improving the Design of Existing Code</title>
		<link>http://joelhooks.com/2008/05/24/refactoring-improving-the-design-of-existing-code/</link>
		<comments>http://joelhooks.com/2008/05/24/refactoring-improving-the-design-of-existing-code/#comments</comments>
		<pubDate>Sat, 24 May 2008 17:25:02 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[object oriented concepts]]></category>
		<category><![CDATA[software architecture]]></category>

		<guid isPermaLink="false">http://joelhooks.com/2008/05/24/refactoring-improving-the-design-of-existing-code/</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
 My code smells bad. There is no doubt about it. As soon as I read that metaphor my head was nodding with understanding. This book is a classic, and I recommend it to anybody that wants to improve their coding habits and create more flexible applications that don't make you sad and angry.  It is [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<p align="center"><a href="http://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Technology/dp/0201485672"><img src="http://69.164.207.135/wp-content/uploads/2008/05/refactoring_cover.jpg" alt="refactoring_cover.jpg" height="298" width="232" /></a></p>
<p align="left"> My code smells bad. There is no doubt about it. As soon as I read that metaphor my head was nodding with understanding. <a href="http://www.amazon.com/Refactoring-Improving-Existing-Addison-Wesley-Technology/dp/0201485672">This book</a> is a classic, and I recommend it to anybody that wants to improve their coding habits and create more flexible applications that don't make you <strong><font color="#3366ff">sad</font></strong> and <strong><font color="#ff0000">angry</font></strong>.</p>
<p align="left"> It is a thick volume, but 2/3s of the text is the catalog of refactorings. This makes the text describing the <a href="http://www.refactoring.com/">concepts of refactoring</a> a relatively short read. Mr Fowler guides you through the process, providing a simple example and clearly explaining the thought processes involved. The catalog covers a wide range of common approaches to refactoring an application.</p>
<p align="left">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/05/24/refactoring-improving-the-design-of-existing-code/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Enumerating data types in Actionscript 3 (AS3)</title>
		<link>http://joelhooks.com/2008/04/28/enumerating-data-types-in-actionscript-3-as3/</link>
		<comments>http://joelhooks.com/2008/04/28/enumerating-data-types-in-actionscript-3-as3/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 16:12:51 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[object oriented concepts]]></category>

		<guid isPermaLink="false">http://joelhooks.com/2008/04/28/enumerating-data-types-in-actionscript-3-as3/</guid>
		<description><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
In computer programming, an enumerated type is an abstract data type used to model an attribute that has a specific number of options (or identifiers) such as the suit of a playing card (i.e. a Club, Diamond, Heart or Spade). Using this type allows the program to handle the attribute more efficiently than a string [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
#leftcontainerBox {
float:left;
position: fixed;
top: 60%;
left: 5px;
}

#leftcontainerBox .buttons {
float:left;
clear:both;
margin:4px 4px 4px 4px;

padding-bottom:2px;
}


#bottomcontainerBox {
height: 30px;
width:50%;
padding-top:1px;
}

#bottomcontainerBox .buttons {
float:left;
height: 30px;
margin:4px 4px 4px 4px;
}

</style>
<blockquote><p>In <a href="http://en.wikipedia.org/wiki/Computer_programming" title="Computer programming">computer programming</a>, an <strong>enumerated type</strong> is an <a href="http://en.wikipedia.org/wiki/Abstract_data_type" title="Abstract data type">abstract data type</a> used to model an attribute that has a specific number of options (or <a href="http://en.wikipedia.org/wiki/Identifier" title="Identifier">identifiers</a>) such as the suit of a playing card (i.e. a Club, Diamond, Heart or Spade). Using this type allows the program to handle the attribute more efficiently than a string while maintaining the readability of the source code.<br />
-from <a href="http://en.wikipedia.org/wiki/Enumerated_type">Wikipedia</a></p></blockquote>
<p>I've found this method (illustrated in code below) for enumeration of datasets to be useful, and at this point even essential, in my applications. Prior to this I was actually cutting and pasting the arrays that I was using in my various combo boxes and other selection components as dataProviders. By breaking the dataset into an enumeration class, it makes it easy to maintain the list and lets the dataset function as a first class citizen, ensuring that your data is consistent throughout the application.</p>

<div class="wp_codebox"><table><tr id="p4556"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
</pre></td><td class="code" id="p45code56"><pre class="actionscript" style="font-family:monospace;">package enum
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/**
	 * CloudTypeEnum enumerates cloud types.
	 * @author JHooks
	 *
	 */</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CloudTypeEnum
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const NONE:CloudTypeEnum			= <span style="color: #000000; font-weight: bold;">new</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;None&quot;</span>,	   -<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const CUMULUS:CloudTypeEnum		= <span style="color: #000000; font-weight: bold;">new</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Cumulus&quot;</span>, 	<span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const STRATUS:CloudTypeEnum		= <span style="color: #000000; font-weight: bold;">new</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Stratus&quot;</span>, 	<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const CIRRUS:CloudTypeEnum		= <span style="color: #000000; font-weight: bold;">new</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Cirrus&quot;</span>, 		<span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const NIMBUS:CloudTypeEnum		= <span style="color: #000000; font-weight: bold;">new</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;Nimbus&quot;</span>, 		<span style="color: #cc66cc;">3</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> value:<span style="color: #0066CC;">String</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> ordinal:<span style="color: #0066CC;">int</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * CloudTypeEnum constructor
		 * @param value
		 * @param ordinal
		 *
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CloudTypeEnum<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span>, ordinal:<span style="color: #0066CC;">int</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">value</span> = value;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">ordinal</span> = ordinal;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * A list of cloud types
		 * @return
		 *
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> <span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span> CUMULUS,
					 STRATUS,
					 CIRRUS,
					 NIMBUS <span style="color: #66cc66;">&#93;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * A list of cloud types appropriate for use in ComboBox and other
		 * selection components as a DataProvider
		 *
		 * myComboBox.dataProvider = CloudTypeEnum.cList;
		 * @return
		 *
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> cList<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">list</span>.<span style="color: #0066CC;">unshift</span><span style="color: #66cc66;">&#40;</span> NONE <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		 * Select a cloud type by its value property
		 * @param value
		 * @return
		 *
		 */</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> selectByValue<span style="color: #66cc66;">&#40;</span> value:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:TaskTypeEnum
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> cloudType:TaskTypeEnum <span style="color: #b1b100;">in</span> CloudTypeEnum.<span style="color: #0066CC;">list</span> <span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> value == cloudType.<span style="color: #006600;">value</span> <span style="color: #66cc66;">&#41;</span>
					<span style="color: #b1b100;">return</span> cloudType;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">return</span> NONE;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Thanks to <a href="http://content.futurescale.com/">Cliff Hall</a> who showed this to me via one of his PureMVC demo applications. For the life of me I can't remember which one.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/04/28/enumerating-data-types-in-actionscript-3-as3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
	</channel>
</rss>

