Flex

Prana Framework



The Prana Framework is a dependency injection framework for ActionScript 3 created by Christophe Herreman.  It's implementation is heavily influenced by the Spring Framework for Java.  Prana Framework is maintained at http://www.pranaframework.org/

The Dependency Injection design pattern is very well documented at http://www.martinfowler.com/articles/injection.html.  In summary, dependency injection (or DI for short) is very helpful for assembling loosely coupled applications.  The DI container takes on the responsibility of creating the application's objects and managing each managed object's lifecycle.  Moreover, the DI container manages collaboration dependencies between objects by wiring these objects together by using either constructor or setter injection.  DI promotes the use of interface-based programming, specifying collaborations as interface contracts.  By maintaining the responsibility of creating objects and wiring collaborators together within the DI container, users of DI can easily unit test objects.  In the unit test scenario, the unit test itself wires the dependencies of the object under test, typically using test doubles (stub objects, fakes, test spies, mock objects) for the dependency implementations.

API documentation (0.5 version)


Accessing the Prana ApplicationContext within your Flex app


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    creationComplete="onCreationComplete()">

<mx:Script>
    <![CDATA[
        import org.pranaframework.context.support.XMLApplicationContext;
       
       
        private var _applicationContext:XMLApplicationContext;

        private function onCreationComplete() : void {
            _applicationContext = new XMLApplicationContext("application-context.xml");
            _applicationContext.addEventListener(Event.COMPLETE, onApplicationContextComplete);
            _applicationContext.load();
        }

        private function onApplicationContextComplete(event:Event):void {
            // Reference Prana-maintained servcies.
        }
      
    ]]>
</mx:Script>   

...
  
</mx:Application>




The Prana Framework application context XML file

The application context file looks remarkably similar to the XML format from the Spring Framework.  You will not have any difficulty with this format if you have already worked with Spring or Spring.NET.

<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.pranaframework.org/objects"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.pranaframework.org/objects http://www.pranaframework.org/schema/objects/prana-objects-0.5.xsd">

    <object id="applicationController" class="org.pranaframework.cairngorm.CairngormFrontController">
        <constructor-arg>
            <object>
                  <property name="login" value="LoginCommand"/>
              </object>
          </constructor-arg>
          <constructor-arg value="goodeats.command"/>
    </object>
</objects>