The Prana IoC Container

1,896 views

Published on

Talk about the Prana IoC Container at the Adobe AIR Pre-Release tour in Gent, Belgium (26/01/2008)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,896
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

The Prana IoC Container

  1. 1. Christophe Herreman www.herrodius.com Adobe Usergroup Belgium 26 january 2008
  2. 2. <ul><li>What is Prana ? </li></ul><ul><li>An Inversion of Control (IoC) Container for ActionScript 3.0 </li></ul><ul><li>Based on Java Spring </li></ul><ul><li>Current release 0.4 (released today) </li></ul><ul><li>www.pranaframework.org </li></ul>
  3. 3. <ul><li>What is Prana ? </li></ul><ul><li>Cairngorm support </li></ul><ul><li>PureMVC support </li></ul><ul><li>Reflection API </li></ul><ul><li>Several Utilities </li></ul>
  4. 4. <ul><li>What is Inversion of Control ? </li></ul><ul><li>Inversion of Control, also known as IOC, is an object-oriented programming principle that can be used to reduce coupling inherent in computer programs. </li></ul>
  5. 5. <ul><li>What is Inversion of Control ? </li></ul><ul><li>Scenario: Class A uses class B to get something done </li></ul><ul><li>Class A { </li></ul><ul><li>public function doSomething():* { </li></ul><ul><li>var b:B = new B(); </li></ul><ul><li>var somethingDone:* = b.getSomethingDone(); </li></ul><ul><li>return markAsDone(somethingDone); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Notice : </li></ul><ul><li>can’t switch the implementation of B </li></ul><ul><li>can’t unit test in isolation </li></ul>
  6. 6. <ul><li>Types of Inversion of Control ? </li></ul><ul><li>Dependency Lookup </li></ul><ul><ul><li>Dependency Pull </li></ul></ul><ul><ul><li>Contextualized Dependency Lookup </li></ul></ul><ul><li>Dependency Injection </li></ul><ul><ul><li>Constructor Injection </li></ul></ul><ul><ul><li>Setter Injection </li></ul></ul>
  7. 7. <ul><li>Dependency Pull </li></ul><ul><li>Class A { </li></ul><ul><li>public function doSomething():* { </li></ul><ul><li>var b:B = UberModel.getInstance().b; </li></ul><ul><li>var somethingDone:* = b.getSomethingDone(); </li></ul><ul><li>return markAsDone(somethingDone); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>UberModel.getInstance().b = new B(); </li></ul><ul><li>var a:A = new A(); </li></ul><ul><li>a.doSomething(); </li></ul>
  8. 8. <ul><li>Contextualized Dependency Lookup </li></ul><ul><li>Class A { </li></ul><ul><li>public function doSomething(model:UberModel):* { </li></ul><ul><li>var somethingDone:* = model.b.getSomethingDone(); </li></ul><ul><li>return markAsDone(somethingDone); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>var model:UberModel = UberModel.getInstance(); </li></ul><ul><li>var a:A = new A(); </li></ul><ul><li>a.doSomething(model); </li></ul>
  9. 9. <ul><li>Constructor Injection </li></ul><ul><li>Class A { </li></ul><ul><li>public function A(b:B) { </li></ul><ul><li>this.b = b; </li></ul><ul><li>} </li></ul><ul><li>public function doSomething():* { </li></ul><ul><li>var somethingDone:* = b.getSomethingDone(); </li></ul><ul><li>return markAsDone(somethingDone); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>var a:A = new A(new B()); </li></ul><ul><li>a.doSomething(); </li></ul>
  10. 10. <ul><li>Setter Injection </li></ul><ul><li>Class A { </li></ul><ul><li>private var b:B; </li></ul><ul><li>public function set b(value:B):void { </li></ul><ul><li>this.b = value; </li></ul><ul><li>} </li></ul><ul><li>public function doSomething():* { </li></ul><ul><li>var somethingDone:* = b.getSomethingDone(); </li></ul><ul><li>return markAsDone(somethingDone); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>var a:A = new A(); </li></ul><ul><li>a.b = new B(); </li></ul><ul><li>a.doSomething(); </li></ul>
  11. 11. <ul><li>Inversion of Control? </li></ul><ul><li>Prefer setter injection </li></ul><ul><li>Type to interfaces !!! </li></ul><ul><li>Centralized configuration </li></ul><ul><li>Maintainability </li></ul><ul><li>Reusability </li></ul><ul><li>Testability </li></ul><ul><li>... </li></ul><ul><li>Though counter-intuitive at first ! </li></ul>
  12. 12. 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)
  13. 13. 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>
  14. 14. Working with the IoC container <mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; creationComplete=&quot;onCreationComplete()“> ... private function onCreationComplete():void { _objectsLoader = new XmlObjectDefinitionsLoader(); _objectsLoader.addEventListener(ObjectDefinitionsLoaderEvent.COMPLETE, onObjectDefinitionsLoaderComplete); _objectsLoader.load(&quot;application-context.xml&quot;); } private function onObjectDefinitionsLoaderComplete(event:ObjectDefinitionsLoaderEvent):void { var container:ObjectContainer = _objectsLoader.container; var a:A = container.getObject(“a”); } ... </mx:Application>
  15. 15. <ul><li>Conclusion </li></ul><ul><li>Objects defined in external XML file </li></ul><ul><li>Loaded at runtime </li></ul><ul><li>Managed by container </li></ul><ul><li>Deploy your application with different configurations without recompiling! </li></ul>
  16. 16. <ul><li>Resources </li></ul><ul><li>www.pranaframework.org </li></ul><ul><li>www.herrodius.com </li></ul>
  17. 17. Questions?

×