Overview
ASIoC - open source project - is a very simple implementation of Dependency Injection (DI) pattern also known as Inversion of Control (IoC) written in Action Script 2.0. It is similar in design and philosophy to Java Spring Framework.
ASIoC Container allows engineers to intuitively assemble independent and pluggable components into an integrated whole.
About IoC
In order to know the idea of dependency injection / inversion of control pattern, please look into external links. Martin Flower has written a very good article about IoC. I suggest you look there first.
Quick start
To use ASIoC you have to make two steps:
Step 1. Define component which will describe how to initialize instance of your class. This can be done in one of presented way:
a) You can define component in fly:


		// new instance of asioc container
var ioc:IoCContainer = new IoCContainerAdapter();

		// new instance of asioc component
var iocComp:IoCComponent = new IoCComponentAdapter();

		// setting params for ioccomponent
iocComp.setIoCComponentParams( "SimpleClazz",// class id
		"org.asioc.example.SimpleClazz",// class path
		null, 	// factory method name
		null, 	// is singleton
		null);	// is lazy init
		
		// registering instance of ioc componnet		
ioc.registerComponent(iocComp);

b) You can define component in xml file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML PUBLIC "-//ASIOC//DTD ASBEANS 0.1//EN" 
		"http://asioc.sourceforge.net/dtd/asioc-beans.dtd">
	
<asbeans>
<asbean 
	id="SimpleClazz"
	class="org.asioc.example.SimpleClazz"/>
</asbeans>

c) you can also register an existing instance of your class


		// new instance of asioc container
var ioc:IoCContainer = new IoCContainerAdapter();

		// new instance of your class
var obj : SimpleClazz = new org.asioc.example.SimpleClazz();

		// register instance
ioc.registerComponentInstance(obj, "SimpleClazz");

Step 2. Use IoC container to get instance of your class.

 		
var another_obj : SimpleClazz = null;
another_obj = ioc.getComponentInstance("SimpleClazz");

Example
To download source of this example click [ here ].
First of all let's create two classes which we will be using in following example.

Class SimpleSingleton implements singleton pattern.

 	
				
class org.asioc.example.SimpleSingleton {
	var instance : SimpleSingleton =  null;
	
	public function SimpleSingleton() {
	}
	
	public function getInstance() {
		if (this.instance == null) {
			this.instance = new SimpleSingleton();
		}
		
		return this.instance;
	}	
	
	public function getClassName() : String  {
		return "org.asioc.example.SimpleSingleton";
	}	
	
}


Class SimpleClass implements simple java beans object.

 	
class org.asioc.example.SimpleClass {
	var varible : String =  null;
	
	public function getVarible() : String {
		return  this.varible;
	}	
	
	public function setVarible(v : String) {
		this.varible = v;
	}
	
}


Now lets configure our environment.
The first way is to create XML file "asioc-config.xml":

 	
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE asbeans PUBLIC "-//ASIOC//DTD ASBEANS 0.1//EN" 
		"http://asioc.sourceforge.net/dtd/asioc-beans.dtd">
	
<asbeans>

	<asbean
		id="SimpleClass"  
		class="org.asioc.example.SimpleClass" 
	/>
			
	<asbean 
		id="SimpleSingleton"  
		class="org.asioc.example.SimpleSingleton" 
		factory-method="getInstance" 
		singleton="true" 
		lazy-init="true" 
	/>
		
</asbeans>


The second way is to write code to configure in fly our IoC Container.

 TODO: paste source


Ok. Now we are ready to use IoC Container. You can get instance of your classes everywhere you want to. Let's see:

	
import com.webservicesware.ioc.IoCLoadConfigObservable;
import com.webservicesware.ioc.IoCContainer;
import com.webservicesware.ioc.IoCUtil;
import com.webservicesware.ioc.IoCConfig;
import logging.LogManager;

/**
 * @author Rafal Malinowski
 */
 
class org.asioc.example.App implements IoCLoadConfigObservable{
	
	public static function main():Void {
				//disable logging 
		LogManager.getInstance().disableLogging();
		
		var app:App = new App();		
		app.start();
	}
	
	public function start() {
		trace("Start load XML Configuration "+
				" from asioc-config.xml.");
		var ioc:IoCContainer = IoCUtil.getIoCContainer();
		
		var iocfg:IoCConfig = IoCUtil.getIoCConfig();	
		iocfg.addOnLoadConfigListener(this);		
		iocfg.configureIoCContainer();
	}

	public function onIoCLoadConfig() : Void {
		
		trace("On configuration load.");
		var ioc:IoCContainer = IoCUtil.getIoCContainer();
				
		var simpleObj : org.asioc.example.SimpleClass = null;
		simpleObj = ioc.getComponentInstance("SimpleClass");
		trace("simpleObj.getVarible() return: "+
			simpleObj.getVarible());
		
		simpleObj.setVarible("Sample Varible");
		
		var simpleObj2 : org.asioc.example.SimpleClass = null;
		simpleObj2 = ioc.getComponentInstance("SimpleClass");
		trace("simpleObj2.getVarible() return: "+
			simpleObj2.getVarible());
		
		var single : org.asioc.example.SimpleSingleton = null;
		single = ioc.getComponentInstance("SimpleSingleton");
		trace("single.getClassName() return "+
			single.getClassName());
	}

}


If you run this example you should see something like this:


Start loading XML Configuration from asioc-config.xml.
Start testing IoC Container.

simpleObj.getVarible() 
	return:	null

simpleObj2.getVarible() 
	return:	Sample Varible

sSingletonObj.getClassName() 
	return:	org.asioc.example.SimpleSingleton
					

Requirements
To use IoC Container you shold have
AS2Lib - Support the Java concept of reflection [ more ]
Log4as - Logger for Action Script similar to Log4J
[ more ]



Download
Current Releases
Current version is 1.0 [ download ]
Requirements
AS2Lib - Support the Java concept of reflection [ more ]
Log4as - Logger for Action Script similar to Log4J
[ more ]

Documentation
Version 1.0
Api documentation for ASIoC ver 1.0 (for now only Polish version , english will be soon...) [ more ]

About me
rmalinowski.pl - my home page [ visit ]

My other projects
Sock 4 Log is a java listening tool, created to make remote logging simpler. It's created for retrieving xml log data from Flash, PHP, JavaScript and other....   [ more ]

GWT 2 SWF intend to provide software bridge between GWT and FLASH/FLEX. It's made for communication between GWT and FLASH/FLEX   [ more ]

GWT Reflection is a small framework which give you ability to use reflection API on client (JavaScript) side of application made by Google Web Toolkit.    [ more ]


 
SourceForge.net Logo