It's an implementation of the pattern as described by Martin Fowler in “Inversion of Control Containers and the Dependency Injection pattern”. It contains setter injection and constructor injection implementations. Main ideas are to :
- connect simple components in depedency injection container,
- make use of Flex features e.g. use MXML files (with code completition etc.) rather than external XML files for depedency injection configuration,
This container can be used also as a service locator.

Simple example of configuration:
<?xml version="1.0" encoding="utf-8"?>
<InjectionContainer xmlns="edu.ics.framework.injection.
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:actions="edu.ics.framework.actions.>
<DIComponent id="component1" scope="application" componentClass="{ExplicitCommandImpl}"
>
<constructorParams>
<mx:Array>
<mx:String>string parameter</mx:String>
<mx:Number>2</mx:Number>
<mx:Object>{component2}</mx:Object>
</mx:Array>
</constructorParams>
</DIComponent>
<DIComponent id="component2" scope="application" componentClass="{SimpleComponent}" />
<actions:AbstractAction id="action1" name="addAction" />
<actions:AbstractAction id="action2" name="copyAction"/>
</InjectionContainer>
Every child object is registered as a component with it's
id as a key.
Actually as
InjectionContainer uses reflection, every public and bindable
property is registered.
Using DIComponent DIComponent is basig object for creating components. Basic attributes are:
id - must be specified to identify component; used as a key;
scope - scope of component; three values are allowed:
- application : only one component instance exists for whole application,
- context : there is one component instance for every context,
- instance : new instance is created every time component is requested.
Default value is application
componentClass - a class of component. Remember to use Class object, not just class name.
As in the example componentClass="{SimpleComponent}" .
setterTemplate - object that contains params to inject in created component instance
constructorParams - array of construcotor parameters
Any other attribute of
DIComponent tag will be used for setter injection.
These parameters overrides
setterTemplate object properties.
Example:
<DIComponent id="component2" scope="application" componentClass="{SimpleComponent}" />
<DIComponent id="component3" scope="application" componentClass="{SimpleComponent2}" param1="param1Val" param2="99" />
setterTemplate is used to pass object, that contains params to inject in created component instance.
Example of usage:
<mx:Script>
<![CDATA[
private var setterTemplate3:Object =
{
param1:"param1Value",
param2:99
}
]]>
</mx:Script>
<DIComponent id="component3" scope="application" componentClass="{SimpleComponent2}" setterTemplate="{setterTemplate3}" />
The example above makes the same, that declaration in previous example:
<DIComponent id="component3" scope="application" componentClass="{SimpleComponent2}" param1="param1Val" param2="99" />
which is simpler and preferred method.
In these examples
SimpleComponent2 can be:
package poc
{
public class SimpleComponent2
{
public var param1:String;
private var _param2:Number;
public function set param2(val:Number):void
{
this._param2 = val;
}
public function get param2():Number
{
return this.param2;
}
}
}
Constructor injectionYou can use
constructorParams property to pass array of parameters to constructor.
You can pass up to 15 parameters. Example of usage :
<DIComponent id="component1" scope="application" componentClass="{ExplicitCommandImpl}"
>
<constructorParams>
<mx:Array>
<mx:String>string parameter</mx:String>
<mx:Number>2</mx:Number>
<mx:Object>{component2}</mx:Object>
</mx:Array>
</constructorParams>
</DIComponent>
Simple objectsExample:
<actions:AbstractAction id="action1" name="addAction" />Objects like this are registered with
WrapperComponent . The scope of the component is always
application.
Injection and bindingTo perform injection use Flex binding. You can put binded component into dynamic parameters, setter template or construcor parameters.
Injected object will be resolved during creaton of requested component according to its
scope attribute.
In case of
context scope, the scope name is propagated from parent component request.