Home  >  

AS3 Dependency Injection Demystified

Author photo
| | Comments (6)
AddThis Social Bookmark Button

Dependency Injection (DI) and Inversion of Control (IoC) are design patterns that have been receiving a lot of attention recently in the ActionScript 3 framework community. These patterns provide a solid basis for creating loosely coupled, testable code. This article makes the assumption that the reader is familiar with the concept of design patterns and object oriented programming in ActionScript 3.

What is Dependency Injection?

Dependency Injection is a design pattern that was first described by Martin Fowler. He states that he felt the need for a more specific definition of the Inversion of Control pattern. The problem with Inversion of Control is that it can tend to be too generic.

At the simplest, Dependency Injection is that act of supplying or objects with their instance variables or properties. When you pass a variable to the constructor of a class, you are using Dependency Injection. When you set a property on a class, you are using Dependency Injection. If you aren't coding your AS3 in a strictly procedural or linear fashion, the odds are that you are making use of Dependency Injection right now.

When we refer to Inversion of Control we are looking at the framework around our classes that support the injection of our dependencies. This can come in many forms depending on the framework. Popular frameworks use metadata, binding, xml configuration, or a combination of these approaches to "invert" the creation of dependencies from inside a class object to an external configuration or injection mechanism. Frameworks provide a defined structure to support this mechanism in a consistent manner

Types of Dependency Injection

There are three types of dependency injection. All of these approaches are more than likely familiar to you. The first, constructor injection, is passing variables that set properties on your object via the constructor. Setter injection is using a method on your object to set a property. Property injection is accessing a public property directly and applying a value.

Constructor Injection:

var myObject:MyObject = new MyObject(aDependencyImplementation);

Setter Injection:

var myObject:MyObject = new MyObject();
myObject.setDependsOn(aDependencyImplementation);

Property Injection:

var myObject:MyObject = new MyObject();

myObject.dependsOn =  aDependencyImplementation;

What's in it for me?

There are numerous advantages to writing loosely coupled classes that do a single job and aren't tied to specific implementations of their dependencies. When dependencies aren't injected, when they are created internally by the object using the new Object() syntax, the class is internally bound to the dependency. When this dependency is provided to the class externally the responsibility of creating the dependency is removed from the class. The advantages of this approach are even more significant when we use interfaces instead of specific implementations for our class dependencies. When interfaces are used, the class doesn't care about the specific implementations provided as dependencies as long as they conform to a specific interface.

This approach, Dependency Injection of interfaces instead of implementations, is extremely valuable when we want to unit test our application. As an example, your class uses a WebService class that access your custom Python backend to provide your application with dynamic data from a database. Instead of depending on MyPythonWebService, which actively attempts to access a server and retrieve live data, the class depends on IWebService. When we unit test, we don't want to rely on the server or live data. This is more of a function of integration testing. Instead we want to use mock data stored locally with our application. Since our class expects IWebService, we can supply it with a MockTestWebService class that implements IWebService and provides the same functionality as the MyPythonWebService class with the exception of the data source.

Where to go from here?

There are several Flash/Flex/AS3 frameworks that are implemented as Inversion of Control/Dependency Injection containers:
Robotlegs: Want a framework like PureMVC but without Singletons, Service Locators, or casting? Perhaps one with Dependency Injection and Automatic Mediator Registration? Well, you might enjoy RobotLegs AS3: yet another lightweight micro-architecture for Rich Internet Applications..
Swiz: - Swiz is a framework for Adobe Flex that aims to bring complete simplicity to RIA development. Swiz provides Inversion of Control, event handing, and simple life cycle for asynchronous remote methods. In contrast to other major frameworks for Flex, Swiz imposes no JEE patterns on your code, no repetitive folder layouts, and no boilerplate code on your development. Swiz represents best practices learned from the top RIA developers at some of the best consulting firms in the industry, enabling Swiz to be simple, lightweight, and extremely productive.
Parsley: Parsley is an application framework for Flex/Flash/AIR applications written in AS3.
Spring Actionscript: 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 MVCS framework, extensions for the Cairngorm and PureMVC frameworks and several other utilities.

The above list is utilizing the descriptions from the various framework websites and is by no means complete.

Some further reading on the Dependency Injection design pattern:
Martin Fowler discusses Inversion of Control containers and Dependency Injection Depedency Injection & Testable Objects Designing Reusable Classes [Not specific to DI, but still a good read]

Read more from Joel Hooks. Joel Hooks's Atom feed jhooks on Twitter

Comments

6 Comments

HydraFramework is yet another. I pimp it because it's mine, and it works for us at andCulture. PureMVC-style MVC implementation, and a simple DelegateRegistry for DI. Lightweight.

Amy said:

Another kind of dependency injection you see in the Data Visualization components is injecting property infrmation through styles.

The framework descriptions are hilarious. Bit of a slant for Swiz there, eh? :-)

also, you didn't list Mate.

Joel Hooks said:

I actually just cut-n-pasted the framework descriptions from their respective websites. It wasn't meant to be a comprehensive list of frameworks, just the ones I am familiar with enough to recommend. Hopefully people will continue to list their favorites that didn't make my incomplete list here in the comments.

Leave a comment


Tag Cloud

Question of the Week: Dream App

If you had an unlimited budget and unlimited resources what application would you build and why would you build it?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.