« Scotland | Main | Flashforward Boston Ant Talk »

Inversion of Control in Flex

At the recent BFPUG Design Patterns Meeting I gave an overview of Inversion of Control. Unfortunately due to time constraints I wasn't able to also put together a compelling example and as such I won't be posting the lame example I did put together. Instead I'll point you to an example that demonstrates the concepts and gives you something more to play with.

The ReviewTube source contains an example of IoC. The main file to look at is "com/joeberkovitz/reviewtube/services/ReviewTubeComponents.mxml" which is pulled in from "ReviewTube.mxml".

The key concept is that by using MXML style bindings, you declare a component once and give it a name through the id property. Now everywhere else that you need to reference that component, standard MXML binding syntax can reference that component. The use of setter dependency injection makes it all work. A couple of points not captured in the slides that should be pointed out.

  1. Make sure the MXML file that defines your components is the first child of your main application. That way the creation of everything gets kicked off as soon as possible.
  2. You will probably want to put a creationComplete handler on the MXML file to indicate that component configuration is done and your main application can start.
  3. Components should not rely on the order that their dependent components are set.

Tags: design flex ioc

Comments

This looks quite interesting, but I can't see what I'm looking at in the ReviewTube source, is there any chance you could expand on this with an example?
Here is an implementation of IoC in Flex - http://code.google.com/p/legato/ It has MXML configuration and metadata based injection.