17
Christophe Herreman www.herrodius.com Adobe Usergroup Belgium 26 january 2008

The Prana IoC Container

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: The Prana IoC Container

Christophe Herremanwww.herrodius.com

Adobe Usergroup Belgium26 january 2008

Page 2: The Prana IoC Container

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

Page 3: The Prana IoC Container

What is Prana ?

- Cairngorm support- PureMVC support- Reflection API- Several Utilities

Page 4: The Prana IoC Container

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.

Page 5: The Prana IoC Container

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

Page 6: The Prana IoC Container

Types of Inversion of Control ?

- Dependency Lookup- Dependency Pull- Contextualized Dependency Lookup

- Dependency Injection- Constructor Injection- Setter Injection

Page 7: The Prana IoC Container

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();

Page 8: The Prana IoC Container

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);

Page 9: The Prana IoC Container

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();

Page 10: The Prana IoC Container

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();

Page 11: The Prana IoC Container

Inversion of Control?

- Prefer setter injection- Type to interfaces !!!- Centralized configuration- Maintainability- Reusability- Testability- ...- Though counter-intuitive at first !

Page 12: The Prana IoC Container

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)

Page 13: The Prana IoC Container

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>

Page 14: The Prana IoC Container

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>

Page 15: The Prana IoC Container

Conclusion

- Objects defined in external XML file- Loaded at runtime- Managed by container- Deploy your application with different

configurations without recompiling!

Page 16: The Prana IoC Container

Resources

- www.pranaframework.org- www.herrodius.com

Page 17: The Prana IoC Container

Questions?