Riacon swiz


Published on

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.

Published in: Technology, Education
1 Comment
  • I also have code samples to accompany this deck. Ping me if you need them, or want a full presentation to your group or conference.
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • 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
  • setValue, setNumber, setBoolean, setString, etc
  • Riacon swiz

    1. 1. Introduction to Swiz<br />It’s easy. Seriously.No, I mean it. It is.<br />1<br />Nicholas Tunney<br />
    2. 2. Session Goals<br />What problems are we trying to solve?<br />How do I set up Swiz?<br />How do I put Swiz to work for me?<br />Where else can I learn about Swiz?<br />2<br />
    3. 3. What problems are we trying to solve?<br />3<br />
    4. 4. Inversion of Control<br />Decentralize application control<br />Provide for actions on setup and teardown<br />Separate the WHAT from the WHEN<br />4<br />
    5. 5. Inversion of Control<br />DANGER!<br />5<br />Courtesy of Joam Tall (Flickr)<br />
    6. 6. Dependency Injection<br />Objects are dependent on other objects<br />Objects need those objects to be created (instantiated) to perform a task<br />Objects that handle their own object creation are said to be ‘tightly coupled’<br />A simple change to the program results in major code changes, just not flexible<br />Problem is best illustrated with an example<br />6<br />
    7. 7. Dependency Injection<br />public class Boat {<br />private varengine:Engine= new Merc230();<br /> public function boat () {<br />}<br /> cruise (int RPM):String {<br />engine.go(RPM);<br /> return engine.thrillFactor;<br />}<br />}<br />7<br />
    8. 8. Dependency Injection<br />public class Boat {<br />private var_engine:Engine;<br /> public function boat () {<br />}<br />set Engine (engine:Engine):void {<br /> _engine = engine;<br />}<br /> cruise (int RPM):String {<br /> _engine.go(RPM);<br /> return _engine.thrillFactor;<br />}<br />}<br />8<br />
    9. 9. Dependency Injection<br />myBoat:IBoat = new Boat();<br />myEngine:IEngine = new Merc230();<br />myBoat.setEngine(myEngine);<br />myBoat.go(3000) // ‘w00t!’<br />myNewEngine:IEngine = new Merc57();<br />myBoat.go(3000) // ‘YEEEHHHHHAAAA!!!’<br />9<br />
    10. 10. Dependency Injection<br />Can see how powerful dependency injection can be<br />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)<br />10<br />
    11. 11. Singletons<br />Create once, use everywhere<br />Low overhead, single instantiation<br />Swiz is used to create these objects, and handle keeping state across the application<br />11<br />
    12. 12. Creation of Transient Objects<br />Swiz permits Prototyping objects<br />Acts like an object factory<br />Ask Swiz for an object, it injects dependencies and hands you a single instance of that object<br />Not persistent<br />12<br />
    13. 13. Event Handling<br />We react to events in the application and perform tasks (WHAT versus WHEN)<br />Event handling in Flex can be a nightmare challenge<br />Events need to be dispatched from non-display objects to varied display objects<br />Events need to be dispatched from display objects to non-display objects like controllers or their presentation models<br />I want to fire an event ANYWHERE in my Flex application, and mediate it ANYWHERE in my Flex application<br />13<br />
    14. 14. Interaction with External Services<br />Every Flex app I have written (except 1) has interacted with external services, it’s the way of the web anymore<br />Each external service call has a set signature and workflow<br />Writing this workflow for every call is tedious, or moreover redundant<br />14<br />
    15. 15. How do I set up Swiz?<br />15<br />
    16. 16. Grab the SWC<br />http://www.swizframework.org<br />Place correct swc in /lib directory<br />16<br />
    17. 17. SwizConfig<br />Brutally simple, just like the framework<br />Can simply copy/paste<br />A few knobs to turn<br />17<br />
    18. 18. SwizConfig<br /><swiz:config><br />        <swiz:SwizConfig<br />            setUpEventType="{ Event.ADDED_TO_STAGE }"<br />            setUpEventPhase="{ EventPhase.CAPTURING_PHASE }"<br />            setUpEventPriority="50"<br />            tearDownEventType="{ Event.REMOVED_FROM_STAGE }"<br />            tearDownEventPhase="{ EventPhase.CAPTURING_PHASE }"<br />            tearDownEventPriority="50"<br />            eventPackages="com.foo.event.*, org.bar.event.*"<br />            viewPackages="com.foo.view.*, org.bar.view.*"<br />            defaultFaultHandler="handleUnhandledFaults"<br />            defaultDispatcher="global" /><br />    </swiz:config><br />18<br />
    19. 19. SwizConfig<br /><swiz:config><br />        <swiz:SwizConfig<br />            eventPackages="com.foo.event.*, org.bar.event.*"<br />            viewPackages="com.foo.view.*, org.bar.view.*” /><br />    </swiz:config><br />19<br />
    20. 20. Logging Target<br />I do not write perfect code, I need to log – a lot<br />Built in logging for Swiz to console<br />Can override and customize by extending the AbstractSwizLoggingTarget class<br /> <swiz:loggingTargets><br />     <swiz:SwizTraceTarget id="myTraceTarget" /><br /> </swiz:loggingTargets><br />20<br />
    21. 21. Stuff We Didn’t Do<br />Verbose configuration<br />Force the usage of J2EE patterns<br />Have to define any special directory structure<br />Tightly couple Swiz to our code (except with ServiceHelper and command chain) – no extending framework classes, etc)<br />Take more than 5 minutes to review the Quickstart ;)<br />21<br />
    22. 22. How do I put Swiz to work for me?<br />22<br />
    23. 23. Review of Problems We Are Solving<br />Inversion of Control<br />Dependency Injection<br />Singletons<br />Creation of Transient Objects (Factory)<br />Event Handling<br />Interaction with External Services<br />23<br />
    24. 24. Inversion of Control<br />Permits us to define and declare dependencies:<br />Controllers – for server interaction<br />Model<br />Delegates<br />Services<br />24<br />
    25. 25. Dependency Injection<br />IoC container is defined by BeanProviders<br />Define non-view classes<br />Once defined, they can all be injected into your UI, presentation model, controllers, etc<br />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<br />25<br />
    26. 26. Dependency Injection<br />Beans.mxml – or whatever you call it<br /><swiz:BeanProvider<br />    xmlns:swiz=" xmlns:service="org.swizframework.quickswiz.service.*"<br />    xmlns:controller="org.swizframework.quickswiz.controller.*"><br /> <br />    <service:UserService id="userService"/><br />    <controller:UserController id="userController"/><br /> <br />    <!-- We'll use the SwizServiceHelper to help simulate a server-side call. --><br />    <swiz:ServiceHelper id="serviceHelper" /><br /> <br /></swiz:BeanProvider><br />26<br />
    27. 27. Dependency Injection<br />So how do we inject these objects? METADATA!<br /> (Note that Swiz can inject into public properties only)<br />Inject by type (preferred)<br />[Inject]<br />variableName:Type<br />Inject by name<br />[Inject (“myService”)]<br />variableName:RemoteObject;<br />27<br />
    28. 28. Dependency Injection<br />Can also inject a bean property<br /> [Inject (“ApplicationConfig.mode”)]<br />currMode:String;<br />Or provide a destination<br />[Inject ( source=“ApplicationConfig.mode”, <br /> destination=“presentationModel.mode” ) ]<br />28<br />
    29. 29. Dependency Injection<br />Setter Injection is also possible<br />[Inject]<br />public function setModel (model:UserModel):void {<br /> …<br />}<br />29<br />
    30. 30. Singletons<br />All objects created in the beans.mxmlas described are officially singletons<br />This statement is not true for prototype objects<br />BEAN LIFECYCLE<br />30<br />
    31. 31. Bean LifeCycle<br />[PostConstruct]<br />[PreDestroy]<br />31<br />
    32. 32. Creation of Transient Objects<br />New object created for each request<br />Much like an object factory<br />Can send in properties via configuration as well<br /><swiz:Prototype id=”User"<br />    type="{ User }"<br />    constructorArguments="{ someOtherBean }" /><br />32<br />
    33. 33. Event Handling<br />Event mediation – decouples the WHAT from the WHEN<br />Code the WHAT<br />Wire the WHEN<br />33<br />
    34. 34. Event Handling<br />Two things we care about<br />Dispatching events<br />Mediating events<br />34<br />
    35. 35. Event Handling<br />Dispatching Events<br />From a UIComponent<br />dispatchEvent()<br />important! The event must bubble!<br />Swiz listens in the root container<br />From other objects<br />Inject a dispatcher<br />[Dispatcher]<br />Variable should have a type of IEventDispatcher<br />Swiz handles the dependency injection<br />35<br />
    36. 36. Event Handling<br />Mediating Events<br />Sooooooo easy!<br />This was my aha moment for using a Flex framework<br />36<br />
    37. 37. Event Handling<br />Simple Event Mediation<br />We can do this since we defined event packages<br />Otherwise, specify fully qualified classname<br /> [EventHandler( event=“EventClass.EVENT_CONSTANT” )]<br /> public function myEventHandler (event:Event): void {<br /> …<br /> }<br />37<br />
    38. 38. Event Handling<br />Swiz inspects the handler and reacts accordingly<br />(note there is no event property in this method)<br /> [EventHandler( event=“EventClass.EVENT_CONSTANT” )]<br /> public function myEventHandler (): void {<br /> …<br /> }<br />38<br />
    39. 39. Event Handling<br />Grab properties from the event<br />(note that we can now explicitly call this method outside of an event)<br /> [EventHandler( event=“MyEvent.EVENT”, properties=“user” )]<br /> public function myEventHandler (user:User): void {<br /> …<br /> }<br />39<br />
    40. 40. Event Handling<br />Mediate multiple events with the same handler<br /> [EventHandler( event=“MyEvent.EVENT”]<br /> [EventHandler( event=“MyEvent.OTHER_EVENT”]<br /> public function myEventHandler (): void {<br /> …<br /> }<br />40<br />
    41. 41. Interaction with External Services<br />Swiz provides beans for Async and other services<br />Async: ServiceHelper<br />Non Async: URLRequestHelper<br />41<br />
    42. 42. Interaction with External Services<br />ServiceHelper<br />Define the bean<br />Inject the bean<br />Call the service<br />Handle the results<br />42<br />
    43. 43. Interaction with External Services<br />Define the ServiceHelper bean<br /> <swiz:ServiceHelper id="serviceHelper" /><br />Inject the bean<br />[Inject]<br /> public varsh:ServiceHelper;<br />Call the service<br />sh.executeServiceCall(ro.method(), resultHandler, faultHandler);<br />43<br />
    44. 44. Demo<br />Demonstrates everything we have talked about today, plus supports the Cat Internet meme.<br />44<br />
    45. 45. Bonus: Client Persistence<br />SharedObject- Provides methods for storing items<br /><swiz:BeanProvider<br />    xmlns:swiz="<br />    xmlns:storage="org.swizframework.storage.*”><br />    <storage:SharedObjectBean id="soBean" /><br /></swiz:BeanProvider><br />45<br />
    46. 46. Bonus: Client Persistence<br />Using the SharedObject bean<br />[Inject]<br />public varso:ISharedObjectBean;<br />  <br />[Bindable]<br />public function get appIndex():int<br />{<br />    // the second parameter is the initial value<br />    return so.getInt("appIndex", 0);<br />}<br />  <br />public function set appIndex(index:int):void<br />{<br />    so.setInt("appIndex", index);<br />}<br />46<br />
    47. 47. Where else can I learn about Swiz?<br />47<br />
    48. 48. Continued Learning<br />http://swizframework.org<br />Starting point for everything<br />Wiki – everything in this preso is on the wiki<br />Quickstart<br />Configuration<br />FAQ<br />Everything else<br />@swizframework<br />Google Group – <br />groups.google.com/group/swiz-framework<br />48<br />
    49. 49. 49<br />Thanks!<br />@ntunney<br />blog.nictunney.com<br />nic.tunney@nictunney.com<br />bind53@gmail.com<br />