Presentation on getting started with the Swiz framework for Adobe Flex. It concentrates on solving problems around IoC, dependency injection, event handling and interacting with external services.
6. Dependency Injection Objects are dependent on other objects Objects need those objects to be created (instantiated) to perform a task Objects that handle their own object creation are said to be ‘tightly coupled’ A simple change to the program results in major code changes, just not flexible Problem is best illustrated with an example 6
7. Dependency Injection public class Boat { private varengine:Engine= new Merc230(); public function boat () { } cruise (int RPM):String { engine.go(RPM); return engine.thrillFactor; } } 7
8. Dependency Injection public class Boat { private var_engine:Engine; public function boat () { } set Engine (engine:Engine):void { _engine = engine; } cruise (int RPM):String { _engine.go(RPM); return _engine.thrillFactor; } } 8
9. Dependency Injection myBoat:IBoat = new Boat(); myEngine:IEngine = new Merc230(); myBoat.setEngine(myEngine); myBoat.go(3000) // ‘w00t!’ myNewEngine:IEngine = new Merc57(); myBoat.go(3000) // ‘YEEEHHHHHAAAA!!!’ 9
10. Dependency Injection Can see how powerful dependency injection can be Can do that in various places across files (still lots of code to change, or via configuration using a framework like Swiz (one file to change) 10
11. Singletons Create once, use everywhere Low overhead, single instantiation Swiz is used to create these objects, and handle keeping state across the application 11
12. Creation of Transient Objects Swiz permits Prototyping objects Acts like an object factory Ask Swiz for an object, it injects dependencies and hands you a single instance of that object Not persistent 12
13. Event Handling We react to events in the application and perform tasks (WHAT versus WHEN) Event handling in Flex can be a nightmare challenge Events need to be dispatched from non-display objects to varied display objects Events need to be dispatched from display objects to non-display objects like controllers or their presentation models I want to fire an event ANYWHERE in my Flex application, and mediate it ANYWHERE in my Flex application 13
14. Interaction with External Services Every Flex app I have written (except 1) has interacted with external services, it’s the way of the web anymore Each external service call has a set signature and workflow Writing this workflow for every call is tedious, or moreover redundant 14
20. Logging Target I do not write perfect code, I need to log – a lot Built in logging for Swiz to console Can override and customize by extending the AbstractSwizLoggingTarget class <swiz:loggingTargets> <swiz:SwizTraceTarget id="myTraceTarget" /> </swiz:loggingTargets> 20
21. Stuff We Didn’t Do Verbose configuration Force the usage of J2EE patterns Have to define any special directory structure Tightly couple Swiz to our code (except with ServiceHelper and command chain) – no extending framework classes, etc) Take more than 5 minutes to review the Quickstart ;) 21
23. Review of Problems We Are Solving Inversion of Control Dependency Injection Singletons Creation of Transient Objects (Factory) Event Handling Interaction with External Services 23
24. Inversion of Control Permits us to define and declare dependencies: Controllers – for server interaction Model Delegates Services 24
25. Dependency Injection IoC container is defined by BeanProviders Define non-view classes Once defined, they can all be injected into your UI, presentation model, controllers, etc Can define in main application file, best practice to define and include <- but Swiz doesn’t try to force you to do this ;) - Use the <swiz:Bean /> tag 25
26. Dependency Injection Beans.mxml – or whatever you call it <swiz:BeanProvider xmlns:swiz=" xmlns:service="org.swizframework.quickswiz.service.*" xmlns:controller="org.swizframework.quickswiz.controller.*"> <service:UserService id="userService"/> <controller:UserController id="userController"/> <!-- We'll use the SwizServiceHelper to help simulate a server-side call. --> <swiz:ServiceHelper id="serviceHelper" /> </swiz:BeanProvider> 26
27. Dependency Injection So how do we inject these objects? METADATA! (Note that Swiz can inject into public properties only) Inject by type (preferred) [Inject] variableName:Type Inject by name [Inject (“myService”)] variableName:RemoteObject; 27
28. Dependency Injection Can also inject a bean property [Inject (“ApplicationConfig.mode”)] currMode:String; Or provide a destination [Inject ( source=“ApplicationConfig.mode”, destination=“presentationModel.mode” ) ] 28
29. Dependency Injection Setter Injection is also possible [Inject] public function setModel (model:UserModel):void { … } 29
30. Singletons All objects created in the beans.mxmlas described are officially singletons This statement is not true for prototype objects BEAN LIFECYCLE 30
32. Creation of Transient Objects New object created for each request Much like an object factory Can send in properties via configuration as well <swiz:Prototype id=”User" type="{ User }" constructorArguments="{ someOtherBean }" /> 32
33. Event Handling Event mediation – decouples the WHAT from the WHEN Code the WHAT Wire the WHEN 33
34. Event Handling Two things we care about Dispatching events Mediating events 34
35. Event Handling Dispatching Events From a UIComponent dispatchEvent() important! The event must bubble! Swiz listens in the root container From other objects Inject a dispatcher [Dispatcher] Variable should have a type of IEventDispatcher Swiz handles the dependency injection 35
36. Event Handling Mediating Events Sooooooo easy! This was my aha moment for using a Flex framework 36
37. Event Handling Simple Event Mediation We can do this since we defined event packages Otherwise, specify fully qualified classname [EventHandler( event=“EventClass.EVENT_CONSTANT” )] public function myEventHandler (event:Event): void { … } 37
38. Event Handling Swiz inspects the handler and reacts accordingly (note there is no event property in this method) [EventHandler( event=“EventClass.EVENT_CONSTANT” )] public function myEventHandler (): void { … } 38
39. Event Handling Grab properties from the event (note that we can now explicitly call this method outside of an event) [EventHandler( event=“MyEvent.EVENT”, properties=“user” )] public function myEventHandler (user:User): void { … } 39
40. Event Handling Mediate multiple events with the same handler [EventHandler( event=“MyEvent.EVENT”] [EventHandler( event=“MyEvent.OTHER_EVENT”] public function myEventHandler (): void { … } 40
41. Interaction with External Services Swiz provides beans for Async and other services Async: ServiceHelper Non Async: URLRequestHelper 41
42. Interaction with External Services ServiceHelper Define the bean Inject the bean Call the service Handle the results 42
43. Interaction with External Services Define the ServiceHelper bean <swiz:ServiceHelper id="serviceHelper" /> Inject the bean [Inject] public varsh:ServiceHelper; Call the service sh.executeServiceCall(ro.method(), resultHandler, faultHandler); 43
46. Bonus: Client Persistence Using the SharedObject bean [Inject] public varso:ISharedObjectBean; [Bindable] public function get appIndex():int { // the second parameter is the initial value return so.getInt("appIndex", 0); } public function set appIndex(index:int):void { so.setInt("appIndex", index); } 46
48. Continued Learning http://swizframework.org Starting point for everything Wiki – everything in this preso is on the wiki Quickstart Configuration FAQ Everything else @swizframework Google Group – groups.google.com/group/swiz-framework 48
Hard to define - goal is to decentralize to promote reusability and maintainabilityWHAT being - what actions i need to perform, what screens I need to showWHEN being - well, when I need to do those things
Any time we decentralize control, this means that we need to add configuration – they go hand in hand
Dependent on things like children an employee has a car, it drives() him to workEmployee needs to get to work, well, the car knows how to drive him, the employee doesn’tIf the employee object creates the car, it means we are stuck with that car every time we create the employeeWhat if we want a different car, or even a bike!
Boat has a merc 230Every time we ask for a boat, we get a boat with a merc 230When we ask the boat to cruise, it does so by using its engine
Now, what if we instead pass the engine in? We can pass in any engine as long as it extends the engine class, or even better, implements Iengine, and hence has the go() functionNow if I want a larger engine, I pass it in at runtime
See difference in go() with the thrill factor
Swiz helps us to manage DI by handling the configuration and injectionMuch like an object factory
Everywhere I ask for a BoatService,swiz will pass in the same BoatService
Created on the fly
See difference in zip files for flex 3 and 4
Holy crap!
Ok, that’s better
Verbose logging, can see what has been injected, what event will be mediated, etc
J2ee – use command pattern instead of controllerWe can replace our metadata and automatic classes with our own classes and the program will continue running – no framework classes to extendEverything in this preso is in the quickstart, and in the docs, which are SHORT
If it can't find the described event, Swiz will actually generate the code for an Event that will fit the need and spit it out in the console for you to copy and paste if you wanttwo, Flash event handling is all string based, meaning MyEvent.FOO is actually just a string value, "myEventType" or whateverwhich can cause collisions if two events have the same literal string value for the typeSwiz doesn't use the string valuesit actually matches up the event class and the constant to figure out which event handlers to invokeso the event type string collision issue is eliminated
Since there is no event property, we can actually explicitly call myEventHandler() from other places
Properties that would come on the event as the result get pulled off and handed off as arguments
Same method can interpret multiple events – no limit
2 different helpersCalls that return an async token like flash remoting- Calls that do not return an async token, like requesting an image from a URL
Sometimes we need to store variables on the client side, like a user object or some other object we need to maintain state