Prana: configuring Cairngorm’s FrontController
ActionScript, Design Patterns, Flex, Prana Add commentsYesterday I introduced the Prana IoC framework for AS3 I'm working on. The example showed how to create an external configuration file that defines the remote services used in the sample application and configures the ServiceLocator. Today I wanted to go a step further and write a config section for the FrontController.
I introduced the CairngormFrontController class that extends Cairngorm's FrontController and lets you pass in commands to the constructor. It then calls the "addCommand()" method on every command found in the constructor argument which results in a configured FrontController instance.
The following object definition was added to the applicationContext.xml file:
-
<object id="shopController" class="be.indiegroup.prana.ioc.util.CairngormFrontController">
-
<constructor-arg>
-
<object>
-
<property name="getProducts" value="GetProductsCommand"/>
-
<property name="addProductToShoppingCart" value="AddProductToShoppingCartCommand"/>
-
<property name="deleteProductFromShoppingCart" value="DeleteProductFromShoppingCartCommand"/>
-
<property name="filterProducts" value="FilterProductsCommand"/>
-
<property name="sortProducts" value="SortProductsCommand"/>
-
<property name="validateOrder" value="ValidateOrderCommand"/>
-
<property name="validateCreditCard" value="ValidateCreditCardCommand"/>
-
<property name="completePurchase" value="CompletePurchaseCommand"/>
-
</object>
-
</constructor-arg>
-
<constructor-arg value="com.adobe.cairngorm.samples.store.command"/>
-
</object>
The first argument is the object that contains the mapping between the event names and the command classes. The second argument is optional and defines the package where the command classes reside. By specifying this argument, you don't need to define the fully qualified classnames of the commands.
We can now leave out the ShopController class, but beware: we need to make sure that the command classes get compiled in the swf of our application. We can do this by referencing them in our application. We'll also reference the CairngormFrontController class:
-
private var _cairngormFrontController:CairngormFrontController;
-
private var _commands:Array = [GetProductsCommand, AddProductToShoppingCartCommand, DeleteProductFromShoppingCartCommand, FilterProductsCommand, SortProductsCommand, ValidateOrderCommand, ValidateCreditCardCommand, CompletePurchaseCommand];
We can also comment out the reference to the ShopController in the main application file:
-
<!-- the FrontController, containing Commands specific to this appliation -->
-
<!--<control:ShopController id="controller" />-->
We are now able to ignore certain view events by leaving out the mapping from the shopController definition. In this application it probably doesn't make much sense, but occasions may arise where this functionality is wanted.
Download Prana 0.1.1
Prana Framework 0.1.1Prana Framework 0.1.1 with dependenciesCairngorm Store Sample- Download Prana on SourceForge: Prana SourceForge page
Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
Christophe Herreman is a software developer living in Belgium. He's working on Flex based e-learning solutions at
April 11th, 2007 at 4:24 am
hello,i found some problem in your ioc patten.
that as3 compiler mxmlc will not compile your class include to swf, if it havnt been new in your project。
so,it’s i found problem. or, we can use mxmlc -include [class],but it’s so bother.
i’m sorry for my poor english.
April 11th, 2007 at 5:37 am
Hi,
this was mentioned in the post. You actually don’t need to make new instances of the classes that you want to include. Just referencing them will force the compiler to add them to the swf.
private var _commands:Array = [GetProductsCommand, AddProductToShoppingCartCommand, DeleteProductFromShoppingCartCommand, FilterProductsCommand, SortProductsCommand, ValidateOrderCommand, ValidateCreditCardCommand, CompletePurchaseCommand];
April 11th, 2007 at 8:28 am
yeah, i see, thank you .
March 14th, 2008 at 8:31 am
It’s really cool what you’ve done with implementing a IoC framework for ActionScript. Especially the Cairngorm framework is a dinosaur framework that really should be rewritten from the ground up with something like Prana powering it. The amounts of pointless boiler-plate code you have to write with Cairngorm is just ridiculous…
April 17th, 2008 at 8:31 am
Hi,
I read some articles about the reflection in AS3. It is said (and I’ve tried) that when using getDefinitionByName to generate an object(as is used in Utils.forName in Prana), only internal AS classes will succeed. while using custom class as the parameter for the method:getDefinitionByName, error will occur, unless users have declared one object of this custom class.(Please refer to http://weblogs.thekeunster.com/?p=10)
Can you please describe how you deal with this? Thanks
April 17th, 2008 at 9:24 am
Hi Paul,
every object that you want to instantiate dynamically needs a class that is compiled into the swf file. To do this, you need to force compilation of that class by either making a reference to it or by adding it to the compiler settings.
April 21st, 2008 at 10:14 am
that’s it. thanks very much.