Prana: configuring Cairngorm’s FrontController

ActionScript, Design Patterns, Flex, Prana Add comments

Yesterday 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:

XML:
  1. <object id="shopController" class="be.indiegroup.prana.ioc.util.CairngormFrontController">
  2.   <constructor-arg>
  3.     <object>
  4.       <property name="getProducts" value="GetProductsCommand"/>
  5.       <property name="addProductToShoppingCart" value="AddProductToShoppingCartCommand"/>
  6.       <property name="deleteProductFromShoppingCart" value="DeleteProductFromShoppingCartCommand"/>
  7.       <property name="filterProducts" value="FilterProductsCommand"/>
  8.       <property name="sortProducts" value="SortProductsCommand"/>
  9.       <property name="validateOrder" value="ValidateOrderCommand"/>
  10.       <property name="validateCreditCard" value="ValidateCreditCardCommand"/>      
  11.       <property name="completePurchase" value="CompletePurchaseCommand"/>
  12.     </object>
  13.   </constructor-arg>
  14.   <constructor-arg value="com.adobe.cairngorm.samples.store.command"/>
  15. </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:

Actionscript:
  1. private var _cairngormFrontController:CairngormFrontController;
  2. 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:

XML:
  1. <!-- the FrontController, containing Commands specific to this appliation -->
  2. <!--<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.1
  • Prana Framework 0.1.1 with dependencies
  • Cairngorm 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
 

7 Responses to “Prana: configuring Cairngorm’s FrontController”

  1. eascn Says:

    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.

  2. Christophe Says:

    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];

  3. eascn Says:

    yeah, i see, thank you .

  4. Espen Says:

    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…

  5. Paul Says:

    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

  6. Christophe Says:

    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.

  7. Paul Says:

    that’s it. thanks very much.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login