Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center /

Dependency injection with Flex

by Bill Bejeck

Bill Bejeck

Created

16 February 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
dependency injection Flex Flex Builder

Requirements

Prerequisite knowledge

For this article, familiarity with Flex Builder, although not required, would be helpful. Familiarity with object-oriented techniques is assumed as well as strong familiarity with ActionScript 3.0.

User level

Beginning

Required products

  • Flex Builder 3 (Download trial)

Additional Requirements

Flex 3 SDK

  • Download

Eclipse 3.3

  • Try

In previous articles I have written on Flex Development, I have stated that one of the good things about developing applications in Flex is how close to Java development it feels. Not to say that Java is the only language in which to develop, but if you work in a language/platform day in and day out to pay the bills, working with something new that is similar to a language/platform that you already know is a plus in my book. Specifically you will learn:

  • The Interface construct in ActionScript 3.0 and how it is functionally equivalent to Java interfaces.
  • How to use Interfaces in ActionScript 3.0 to achieve dependency injection.
  • How to use a simple form of dependency injection within Flex.

Understanding dependency injection

It is generally considered good object-oriented design to limit the responsibilities of a class to one thing (there may be times when, as a developer, you may need to be flexible with this rule, but I am talking in general terms here). To abide by this rule, most objects work with collaborating objects to fulfill their responsibilities. But where do these collaborating objects come from? Typically, without dependency injection, you would instantiate collaborating objects in your components. This creates tight coupling between objects, which makes your app more difficult to maintain over time and harder to unit-test your components. Dependency injection is, in very rough terms, when you would use a framework or container to "inject" the collaborating objects into the respective objects that need them (see a more formal definition). This ends up being a very powerful pattern. You can develop your objects and test them independently (mocking out collaborators if any) and "wire" them together to build your application. The Spring framework is a ubiquitous DI framework in the Java development world.

I'll use a subset of the Spring Framework, Spring-MVC, as an example. In Spring-MVC you have Controller objects that handle inbound requests from a client; in this case, assume a web-browser. In most web applications today, there is a database or datastore on the back end of the application containing information that needs to be retrieved and displayed to the user. Suppose a user enters a search to list books by author. In the simplest terms, the Controller object accepts the request, pulls the author name out of the request parameter, hands that name off to the BookService object and expects a list of book titles in return, and then sends that list of titles back in response to the user request. The Spring framework handles setting the BookService object into your Controller object (configured through XML or annotations, but that is beyond the scope of this article). And the fact that your BookService object implements a Java interface means that changes to the implementation of the service object are invisible to the Controller object. That is a very good thing. As long as the parameters and return type of the method don't change, the Controller object should not care at all how the service does its job. But what does that have to do with Flex development?

Dependency injection through ActionScript 3.0 interfaces

The Interface construct is what gives dependency injection its power. You have a bunch of objects that rely on each other to do their work, and instead of expecting a particular implementation of an object, which, if changed, would required changes in all dependent objects, the objects are only concerned with the contract specified by the interface, so implementation changes can be made seemlessly and testing is a much easier task. Well, guess what: ActionScript 3.0 supports the same Interface construct that makes writing loosely coupled code so much easier.

One would use Interfaces the same way you would in Java:

  1. Define your interface. An ActionScript 3.0 interface could look like:
package com.example { public interface Controller { public function search(searchParams:Object, callback:Function=null):void } }
  1. Define a class that implements the interface.
package com.example { public class HttpController implements Controller{ private var _httpService:HTTPService= new HTTPService(); public function search(searchParams:Object, callback:Function=null):void { details omitted for clarity } } }

In the example above, you have a Controller interface and an implementation, HttpController, that will be used to communicate with the back end of your application to execute searches. HttpController is using the Flex object HTTPService to do the work of sending the search request and handling the response. You can see that the HttpService object is instantiated in the HttpController class. So why not inject the HttpService class as well? The point to having an interface is that you can change the implementation seamlessly to suit the needs of your application. In this case, if you needed another mechanism for searching, you would simply write a new implementation or modify the existing one.

Dependency injection, Flex-style

Here's where the rubber meets to road, so to speak. You have seen what dependency injection is and its importance in good application design. But how do you achieve that in a Flex application? As an example, say you have created a component to accept user input and display search results. This component is really a container of sorts, so you have extended the Panel class to hold the various objects to complete your search functionality. Additionally, you have defined your component's behavior entirely in an ActionScript class (no <script></script> tags in your MXML files!). Here's an example of what an extended Panel object could look like (with details omitted for clarity) in an ActionScript file:

public class SearchPanel extends Panel{ private _searchController:Controller; public function search(text:String):void { this._searchController.search(text, this.callBackFunction); } public function set searchController(searchController:Controller):void { this._searchController = searchController; } public function get searchController():Conroller { return this._searchController; } }

The salient point from the above example is the instance variable, _searchController of type Controller and the getter and setter methods that have the same name as the variable sans the "_" character. Next, take a look at the MXML file called SearchPanelView.mxml representing the visual properties for your component (again, some details omitted for clarity):

<?xml version="1.0" encoding="utf-8"?> <SearchPanel xmlns="bbejeck.example.*" xmlns:comp="bbejeck.example.*" xmlns:mx="http://www.adobe.com/2006/mxml"> <comp:HttpController id="searchController" /> </SearchPanel>

From the example MXML file above, there are a couple of key points:

  1. The line <comp:HttpController id="searchController" /> defines the controller component to be used by the SearchPanel class. Take note that the class name in the definition is "HttpController," while the type of the _searchController instance variable only expects a type of Controller, it does not matter what the implementation is.
  2. The id attribute matches the name of the getter and setter functions in the SearchPanel ActionScript class.
  3. When the SearchPanelView.mxml file is loaded by the Flex application, the "HttpController" object is instantiated then the corresponding setter method in the SearchPanel class that matches the id attribute is called, injecting your Controller implementation into the SearchPanel class
  4. When you need to change your search functionality, you can either change the HttpController object, or define a new implementation and modify the SearchPanelView.mxml file, either way your SearchPanel code does not need to change at all.

Conclusion

You have seen how to use dependency injection in Flex. The Flex application will inject into your components what objects are defined in the MXML file where the id matches a corresponding setter method. We are essentially getting a basic DI container for free by using Flex.

This leads us to some conclusions:

  • When building applications, instead of focusing on building the application itself, you focus on what individual components are needed to build the application and what these components need to do their jobs. What you end up with is a large amount of "ready to go" components that just need some "callback" or service objects implemented in order to build the application. Code reuse is very high and the time it takes to build new applications is substantially reduced.
  • Unit testing is much easier. Now, if you have components that have interface-based collaborators, when you are testing those components you can manually call the appropriate setter methods and supply mock objects that implement the correct interface but provides expected behavior for the test.

Where to go from here

For a great example of dependency injection used in Flex, make sure to check out the Spark architecture in Gumbo, the next release of the Flex framework.

The examples for this article were pulled from a previous article I have written on Flex development, and the code examples there use the same type of dependency injection discussed here.

Several open-source projects have been developed to provide a formal Dependency Injection framework for Flex development. Some are:

  • Swizframework
  • Prana
  • Smartypants

While there are may excellent books on Flex programming, I have found the following to be particularly helpful:

  • Essential ActionScript 3 by Colin Moock
  • Advanced ActionScript 3 with Design Patterns by Joey Lott and Danny Patterson

More Like This

  • A survey of Inversion of Control frameworks for Flex

Tutorials & Samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex User Forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement