Re-Architecting Flex Cairngorm Application with Spring Actionscript
After working with a several enterprise application , I noticed that as the application grows (with many features into it) complexity increased.
Complexity is a major challenge for large applications, the more collaborations of objects , the more complexity.
In order to keep complexity under control we need to give our application more robust testing coverage and make sure our objects is separated and independent from each other.
The dependency injection approach helps us doing so.
Dependency Injection in a Nutshell
Dependency Injection basically describes separating configuration from implementation. Swapping in and out different object dependencies in different application contexts hasn’t been implemented very cleanly in traditional Object Orientated languages. In light of this, frameworks like the Spring Framework have risen in popularity over the last few years.
Spring ActionScript is an offshoot of the Java-based Spring Framework written in ActionScript 3. It is targeted for the Flash, Flex and AIR platforms. The framework contains an Inversion of Control Container, an Operation and Task API, an EventBus, a metadata handling system, extensions for the Cairngorm and PureMVC frameworks and several other utilities.
Spring Actionscript offers several extensions to the Cairngorm 2 micro-architecture that will make it easier to configure the various objects by the Spring Actionscript IoC container. And some of them will just make life a little easier when working with Cairngorm. What follows is a description of how to setup a configuration file that will allow you to manage the Cairngorm framework externally.
Injecting the MXML dependencies
The mxml components in the UI using mostly the Model ,
So we described our Model class in the application-context as follows:
<object id="modelLocator" class="com.adobe.cairngorm.samples.store.model.ShopModelLocator" scope="singleton" >
<!—OMMITED CONFIGURATION -->
The cairngorm ModelLocator class uses the getInstance factory method here in our model we omitted it, and let the framework implement the singleton mechanism (scope=’singleton’) in that way we don’t need to call always to ModelLocator.getInstance that is a sin in a world of loose coupling.
Now we can inject the model to every mxml component using it with the [Autowired] metadata on the model variable.
Injecting Command dependencies
Instead declaring the Model and the Delegate as internal members of the command we let the Spring-AS framework to inject that properties and this how we can let tests injects its mock delegates for example.
Initialize the CairngormController with the framwork
The cairngorm controller uses the addCommand(commanded, command) method in order to map between commands and events.
We took this definition to the application-context.xml and this give us the ability to change mappings without compiling the application again and give us more flexibility using tests.
The ability of declaring all Cairngorm foundation classes outside the application code give us loose coupling between the application code and the MVC framework, so if tomorrow we want to change our MVC framework we do not need to touch a line in our application code.
Furthermore our application is testable and less complex.