Packagecom.soma.core.di
Classpublic class SomaInjector
InheritanceSomaInjector Inheritance Object
Implements ISomaInjector

Author: Romuald Quantin - www.soundstep.com

Resources: http://www.soundstep.com/downloads/somacore

Actionscript version: 3.0

Copyright: Mozilla Public License 1.1 (MPL 1.1) http://www.opensource.org/licenses/mozilla1.1.php

The SomaInjector Class is an adapter for the injection library used by the framework: Swiftsuspenders. http://github.com/tschneidereit/SwiftSuspenders

Injection is not enabled by default to keep the framework lighter in its basic use. To enable injection, The Soma Class constructor or its setup public method can receive a Class that must extends ISomaInjector. The default framework injector that can be used is SomaInjector.

The SomaInjector instance is accessible using the injector property from the facade (your Soma instance), the wires, the mediators and the commands.

The injector instance can be used directly and/or using metadata tags such as [Inject] or [PostConstruct], see the SwiftSuspenders documentation for more information: https://github.com/tschneidereit/SwiftSuspenders

View the examples

See also

com.soma.core.interfaces.ISomaInjector


Public Properties
 PropertyDefined By
  applicationDomain : ApplicationDomain
ApplicationDomain in use in the injector.
SomaInjector
Public Methods
 MethodDefined By
  
Create an instance of the SomaInjector class.
SomaInjector
  
Creates a child injector.
SomaInjector
  
createInstance(classTarget:Class):Object
Instantiates a new instance from a given class.
SomaInjector
  
dispose():void
Destroys the injector elements.
SomaInjector
  
getInstance(classTarget:Class, name:String):Object
Retrieves or instantiates an instance of the given Class.
SomaInjector
  
Retrieves the injector parent if any.
SomaInjector
  
hasMapping(classTarget:Class, name:String):Boolean
Indicates wether a mapping rule exists for the given Class.
SomaInjector
  
injectInto(instance:Object):void
Performs injection into the given instance.
SomaInjector
  
map(whenAskFor:Class, createClass:Class, name:String):void
Rule that defines the "whenAskFor" to be injected with an instance of the "createClass" Class.
SomaInjector
  
mapSingleton(classTarget:Class, name:String):void
Rule that defines the "classTarget" to be injected with always the same instance.
SomaInjector
  
mapSingletonOf(whenAskFor:Class, useSingletonOf:Class, name:String):void
Rule that defines the "whenAskFor" (usually an interface) to be injected with the same instance "useSingletonOf".
SomaInjector
  
mapToInstance(whenAskFor:Class, instance:Object, name:String):void
Rule that defines the "whenAskFor" to be injected the given instance.
SomaInjector
  
removeMapping(classTarget:Class, name:String):void
Removes a mapping rule exists for the given Class.
SomaInjector
Property Detail
applicationDomainproperty
applicationDomain:ApplicationDomain

ApplicationDomain in use in the injector.


Implementation
    public function get applicationDomain():ApplicationDomain
    public function set applicationDomain(value:ApplicationDomain):void
Constructor Detail
SomaInjector()Constructor
public function SomaInjector()

Create an instance of the SomaInjector class. Automatically created by the framework when the injection is enabled and accessible using the injector property.

Method Detail
createChildInjector()method
public function createChildInjector():ISomaInjector

Creates a child injector. The child injector inherits the rules of its parents.

Returns
ISomaInjector — A ISomaInjector instance.

Example
var child:ISomaInjector = injector.createChildInjector();
         
createInstance()method 
public function createInstance(classTarget:Class):Object

Instantiates a new instance from a given class. This method will always creates a new instance, regardless of any singleton mapping.

Parameters

classTarget:Class — A Class.

Returns
Object — An instance of the Class target.

Example
injector.map(MyClass, MyClass);
var class1:MyClass = injector.createInstance(MyClass) as MyClass;
var class2:MyClass = injector.createInstance(MyClass) as MyClass;
         
dispose()method 
public function dispose():void

Destroys the injector elements.

getInstance()method 
public function getInstance(classTarget:Class, name:String):Object

Retrieves or instantiates an instance of the given Class. This method needs a mapping rule, the instance can be a singleton or a new one.

Parameters

classTarget:Class — A Class.
 
name:String — Injection name.

Returns
Object — An instance of the Class target.

Example
injector.map(MyClass, MyClass);
var class1:MyClass = injector.getInstance(MyClass) as MyClass;
var class2:MyClass = injector.getInstance(MyClass) as MyClass;
         
getParentInjector()method 
public function getParentInjector():ISomaInjector

Retrieves the injector parent if any.

Returns
ISomaInjector — A ISomaInjector instance.

Example
var parent:ISomaInjector = injector.getParentInjector();
         
hasMapping()method 
public function hasMapping(classTarget:Class, name:String):Boolean

Indicates wether a mapping rule exists for the given Class.

Parameters

classTarget:Class — A Class.
 
name:String — Injection name.

Returns
Boolean — A Boolean.

Example
injector.map(MyClass, MyClass)
var value:Boolean = injector.hasMapping(MyClass); // return true
         
injectInto()method 
public function injectInto(instance:Object):void

Performs injection into the given instance.

Parameters

instance:Object — An instance.


Example
package {
    public class Injectee {
        [Inject]
        public var myClass:MyClass;
    }
}
         
injector.map(MyClass, MyClass);
var myClass:MyClass = injector.createInstance(MyClass) as MyClass;
injector.injectInto(myClass);
         
map()method 
public function map(whenAskFor:Class, createClass:Class, name:String):void

Rule that defines the "whenAskFor" to be injected with an instance of the "createClass" Class.

Parameters

whenAskFor:Class — A Class.
 
createClass:Class — A class.
 
name:String — Injection name.


Example
injector.map(MyClass, MyOtherClass)
var myOtherClass:MyOtherClass = injector.getInstance(MyClass) as MyOtherClass;
         
mapSingleton()method 
public function mapSingleton(classTarget:Class, name:String):void

Rule that defines the "classTarget" to be injected with always the same instance.

Parameters

classTarget:Class — A Class.
 
name:String — Injection name.


Example
injector.mapSingleton(MyClass, MyClass);
var myClass:MyClass = injector.getInstance(MyClass) as MyClass;
         
mapSingletonOf()method 
public function mapSingletonOf(whenAskFor:Class, useSingletonOf:Class, name:String):void

Rule that defines the "whenAskFor" (usually an interface) to be injected with the same instance "useSingletonOf".

Parameters

whenAskFor:Class — A Class.
 
useSingletonOf:Class — A class.
 
name:String — Injection name.


Example
injector.mapSingletonOf(IMyClass, MyClass);
var myClass:MyClass = injector.getInstance(MyClass) as MyClass;
         
mapToInstance()method 
public function mapToInstance(whenAskFor:Class, instance:Object, name:String):void

Rule that defines the "whenAskFor" to be injected the given instance.

Parameters

whenAskFor:Class — A Class.
 
instance:Object — An instance.
 
name:String — Injection name.


Example
var myClass:MyClass = new MyClass();
injector.mapToInstance(MyClass, myClass)
var myClassSameInstance:MyClass = injector.getInstance(MyClass) as MyClass;
         
removeMapping()method 
public function removeMapping(classTarget:Class, name:String):void

Removes a mapping rule exists for the given Class.

Parameters

classTarget:Class — A Class.
 
name:String — Injection name.


Example
injector.map(MyClass, MyClass)
var value:Boolean = injector.hasMapping(MyClass); // return true
injector.removeMapping(MyClass);
value = injector.hasMapping(MyClass); // return false
         
Examples
var application:ISoma = new Soma(stage, SomaInjector);
     
var application:ISoma = new Soma();
application.setup(stage, SomaInjector);
     
package  {
    import com.soma.core.interfaces.ISoma;
    import com.soma.core.di.SomaInjector;
    import flash.display.Sprite;
    
    public class Main extends Sprite {
        
        private var _app:ISoma;
        
        public function Main() {
            _app = new SomaApplication(stage, SomaInjector);
        }
        
    }
}
     
package  {
    import com.soma.core.Soma;
    import com.soma.core.interfaces.ISoma;
    import flash.display.Stage;
    
    public class SomaApplication extends Soma implements ISoma {
        public function SomaApplication(stage:Stage, injectorClass:Class) {
            super(stage, injectorClass);
        }
        
    }
}
     
package {
    public class Injectee {
        [Inject]
        public var myClass:MyClass;
        [PostConstruct]
        public funtion test():void {
            trace(myClass);
        }
    }
}
     
injector.mapSingleton(MyClass);
injector.createInstance(Injectee);