the prana ioc container
DESCRIPTION
Talk about the Prana IoC Container at the Adobe AIR Pre-Release tour in Gent, Belgium (26/01/2008)TRANSCRIPT
Christophe Herremanwww.herrodius.com
Adobe Usergroup Belgium26 january 2008
What is Prana ?
- An Inversion of Control (IoC) Container for ActionScript 3.0
- Based on Java Spring- Current release 0.4 (released today)- www.pranaframework.org
What is Prana ?
- Cairngorm support- PureMVC support- Reflection API- Several Utilities
What is Inversion of Control ?
Inversion of Control, also known as IOC, is an object-oriented programming principle that can be used to reduce coupling inherent in computer programs.
What is Inversion of Control ?
Scenario: Class A uses class B to get something done
Class A { public function doSomething():* { var b:B = new B(); var somethingDone:* = b.getSomethingDone(); return markAsDone(somethingDone); }}
Notice :- can’t switch the implementation of B- can’t unit test in isolation
Types of Inversion of Control ?
- Dependency Lookup- Dependency Pull- Contextualized Dependency Lookup
- Dependency Injection- Constructor Injection- Setter Injection
Dependency Pull
Class A { public function doSomething():* { var b:B = UberModel.getInstance().b; var somethingDone:* = b.getSomethingDone(); return markAsDone(somethingDone); }}
UberModel.getInstance().b = new B();var a:A = new A();a.doSomething();
Contextualized Dependency Lookup
Class A { public function doSomething(model:UberModel):* { var somethingDone:* = model.b.getSomethingDone(); return markAsDone(somethingDone); }}
var model:UberModel = UberModel.getInstance();var a:A = new A();a.doSomething(model);
Constructor Injection
Class A { public function A(b:B) { this.b = b; } public function doSomething():* { var somethingDone:* = b.getSomethingDone(); return markAsDone(somethingDone); }}
var a:A = new A(new B());a.doSomething();
Setter Injection
Class A { private var b:B; public function set b(value:B):void { this.b = value; }
public function doSomething():* { var somethingDone:* = b.getSomethingDone(); return markAsDone(somethingDone); }}
var a:A = new A();a.b = new B();a.doSomething();
Inversion of Control?
- Prefer setter injection- Type to interfaces !!!- Centralized configuration- Maintainability- Reusability- Testability- ...- Though counter-intuitive at first !
What is an IoC Container ?
Creates and assembles components/objects and manages their lifecycle.
Prana uses an XML dialect to define the “application context” (what objects are available and how they are related)
The Application Context
// setter injection<objects> <object id=“a” class=“A”> <property name=“b” ref=“b”/> </object> <object id=“b” class=“B”/></objects>
// constructor injection<objects> <object id=“a” class=“A”> <constructor-arg ref=“b”/> </object> <object id=“b” class=“B”/></objects>
Working with the IoC container<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="onCreationComplete()“>
...
private function onCreationComplete():void { _objectsLoader = new XmlObjectDefinitionsLoader(); _objectsLoader.addEventListener(ObjectDefinitionsLoaderEvent.COMPLETE,
onObjectDefinitionsLoaderComplete); _objectsLoader.load("application-context.xml"); }
private function onObjectDefinitionsLoaderComplete(event:ObjectDefinitionsLoaderEvent):void { var container:ObjectContainer = _objectsLoader.container; var a:A = container.getObject(“a”); }
...
</mx:Application>
Conclusion
- Objects defined in external XML file- Loaded at runtime- Managed by container- Deploy your application with different
configurations without recompiling!
Resources
- www.pranaframework.org- www.herrodius.com
Questions?