2. Agenda What is Spring ActionScript The IoC container Configuration MVC Architecture Demo Q&A
3. What is Spring ActionScript(1/2) Inversion of Control (IoC) forActionScript 3.0 Flash/Flex/AIR/Pure AS3 IoC container Tailoredarchitectures (noprescriptivestructure)
4. What is Spring ActionScript(2/2) Started as anin-houselibrary Formerlyknown as the Pranaframework Incubated as a Spring Extension project Upcoming release 1.0 (1.0RC1) AS3Commons: Lang, Logging, Reflect, …
5. The IoC Container (1/2) = Object Factory Creates and assembles objects Centralizeddependency management Spring AS: configuration via XML or MXML
6. The IoC Container (2/2) Object (bean): object managed and/orcreatedby the container Object Factory: factorythatcreates and managesobjects factory.getObject(“myObject”) Object Definition: blueprintforan object Application Context: smarter Object Factory
11. Configuration(5/9) MXML Configuration: cons Compiledinto the application Explicitdeclaration: limited to singletons, noexternalreferences, no prototypes -> use Spring AS MXML dialect
13. Configuration(7/9) XML Configuration: loading var context:XMLApplicationContext = newXMLApplicationContext(); context.addConfigLocation(“context.xml”); context.addEventListener(Event.COMPLETE, context_completeHandler); context.load(); function context_completeHandler(event:Event):void { var appModel:ApplicationModel = context.getObject(“appModel”); var appController:ApplicationController = context.getObject(“appController”); }
14. Configuration(8/9) XML Configuration: pros Richer dialect than MXML config Usablefornon-Flexprojects No need to recompile (in some cases) Familiar to Spring Java users
16. MVC Architecture(1/3) No realprescriptivearchitecture Do youreallyneedone? Rollyourownbecauseone does NOT fit all Many MVC architectures out there: Cairngorm (2), PureMVC, Mate, … … Spring AS wants to support them (seeextensions)
17. MVC Architecture(2/3) Spring AS MVC ingredients Operation API Event Bus Autowiring Recommendations Presentation Model (Fowler) LayeredArchitecture (DDD – Evans)
19. The Operation API (1/7) Asynchronousbehavior in Flash Player: loadingexternal resources, RMI, sqlite, Webservices, HTTPServices, … No threading API Many different APIs: AsyncToken, Responders, Callbacks, Events… we need a unifiedapproach!
20. The Operation API (2/7) Problem: a user repository (or service) interface IUserRepository { functiongetUser(id:int): ??? } What does the getUsermethod return? AsyncToken (Flex/RemoteObject), User (In memory), void (events)
21. The Operation API (3/7) Spring AS solution: return anIOperation interface IUserRepository { functiongetUser(id:int):IOperation; } Cannowbeused in anyActionScript 3 context and easilymockedfortesting
22. The Operation API (4/7) In Spring AS, an “operation” is used to indicateanasynchronousexecution IOperation interface with “complete” and “error” events IProgressOperationwith “progress” event OperationQueuebundlesoperations (a compositeoperation)
23. The Operation API (5/7) Defininganoperation: public classGetUserOperationextendsAbstractOperation { functionGetUserOperation(remoteObject:RemoteObject, id:String) { vartoken:AsyncToken = remoteObject.getUser(id); token.addResponder(newResponder(resultHandler, faultHandler)); } private functionresultHandler(event:ResultEvent):void { dispatchCompleteEvent(User(event.result)); } private functionfaultHandler(event:FaultEvent):void { dispatchErrorEvent(event.fault); } }
24. The Operation API (6/7) Usage: varoperation:IOperation = newGetUserOperation(remoteObject, 13); operation.addCompleteListener(function(event:OperationEvent):void { var user:User = event.result; }); operation.addErrorListener(function(event:OperationEvent):void { Alert.show(event.error, “Error”); });
25. The Operation API (7/7) A progressoperation: varoperation:IOperation = newSomeProgressOperation(param1, param2); operation.addCompleteListener(function(event:OperationEvent):void { }); operation.addErrorListener(function(event:OperationEvent):void { }); operation.addProgressListener(function(event:OperationEvent):void { var op:IProgressOperation = IProgressOperation(event.operation); trace(“Progress:” + op.progress + “, total: “ + op.total); });
26. The Event Bus (1/4) Publish/subscribeevent system Usedforglobalapplicationevents Promotesloosecoupling Works withstandard Flash Events; noneed to subclass Spring AS base classes
27. The Event Bus (2/4) Listening/subscribing to events: // listen for all events EventBus.addListener(listener:IEventBusListener); functiononEvent(event:Event):void { } // listen forspecificevents EventBus.addEventListener(“anEvent”, handler); functionhandler(event:Event):void { }
28. The Event Bus (3/4) Dispatching/publishingevents: // dispatchstandardevent EventBus.dispatchEvent(newEvent(“someEvent”)); // dispatchcustomevent classUserEventextendsEvent { ... } EventBus.dispatchEvent(newUserEvent(UserEvent.DELETE, user));
29. The Event Bus (4/4) Routing events: <object id="routeEventsProcessor" class="org.springextensions.actionscript.ioc.factory.config.RouteEventsMetaDataPostProcessor"/> [RouteEvents] Public classMyClass {} or… [RouteEvents(events=“eventA,eventB”)] Public classMyClass {}
30. Autowiring(1/2) Auto DependencyInjection via metadata Autowireby type, name, constructor, autodetect Works forobjectsmanagedby the container and for view components Be careful: magic happens, noexplicitconfiguration
31. Autowiring(2/2) Annotate a propertywith [Autowired] classUserController { [Autowired] public var userRepository:IUserRepository; } [Autowired(name=“myObject“, property=“prop”)]
35. The Container (4/5) Object definition scopes Singleton (default): sameinstance is returnedoneachrequest Prototype: newinstance is returned <object id=“mySingleton” class=“MyClass”/> <object id=“myPrototype” class=“MyClass” scope=“prototype”/> or… <object id=“myPrototype” class=“MyClass” singleton=“false”/>
36. The Container (5/5) Lazyinitializedobjects Singletonsthat are notinstantiatedwhen the container starts <object id=“myLazyObject” class=“MyClass” lazy-init=“true”/>
37. And there is more… Module support XML namespaces (rpc, messaging, util, …) Task API Service base classes Container extensionpoints (factory post processors, object post processors, custom metadata handlers, …) Workingon: testing support, AOP, …
38. Summary Dependency Management Loosecoupling, type to interfaces Usewithotherframeworks Spring mentality: provide choice Promote best practices