Wednesday, July 7, 2010

Swiz it!


Lets start with a quick video! Then here's a great read on Swiz! Here's still another one! Then a few examples are here 

BTW Swiz is a brain child of  Chris Scott

And of course here's the nice official documentation!

The BIG three offerings of Swiz -
         a) Dependency Injection
         b) Event Handling
         c) Server Interaction!

It does not impose any boilerplate code. It also does not impose J2EE patterns on your code - which may not be so good in my opinion!

Steps in creating a swiz based flex app

1) Swiz Configuration: In the main application file [under declaration section for FB4], define a <swiz:Swiz> metadata tag.

    a) Define your non-visual objects that Swiz should process for dependency injection & event mediation in <swiz:BeanProviders> tag.
 

    b) Define event & view packages in <swiz:config> tag.

     <xml id=simple>

        <swiz:Swiz>
           
            <!-- BeanProviders simply contain the non-display objects that Swiz  should process. -->
            <swiz:beanProviders>
                <config:Beans />
            </swiz:beanProviders>
           
            <swiz:config>
                <!-- The eventPackages value tells Swiz the path to your Event classes,
                and viewPackages is an optional value that speeds up the processing of display classes. -->
                <swiz:SwizConfig
                    eventPackages="org.swizframework.quickswiz.event.*"
                    viewPackages="org.swizframework.quickswiz.view.*" />
            </swiz:config>
           
        </swiz:Swiz>
    </xml>

2) Dependencies Declaration (Services, Controllers, Helpers): In the config package, Define 'Beans.mxml' extending <swiz:BeanProvider>, declare

Service & Controller [& Service Helper]

A Swiz BeanProvider is an MXML file that acts similar to an object registry.
It is the IoC Container of Swiz framework i.e. takes over the task of managing the dependencies from your application.


When you create a bean loader, you’re extending the Swiz BeanProvider class, which registers the objects that you want Swiz to manage for Dependency Injection and Event Mediation.
 

Here you declare objects that will be used in the application and assign IDs to them so that they can be referenced by ID tags at various places where they are needed to be injected(auto-wired).


3) Controller [You define Event handlers for events to be mediated by Flex, declare Service objects to be injected, and make use of service objects in event handlers]: In Controller package, Define a 'Controller.as' class.

    a) Make use of the Service, & ServiceHelper classes here
    b) Declare events which Flex will mediate - using [Mediate(event="EventType", properties="parameter names to be passed to event handling method")] tag

        [Mediate( event="UserEvent.SAVE_USER_REQUESTED", properties="user" )]
        /**
         * Perform a server request to save the user
         */
        public function saveUser( user : User ) : void
        {
            serviceHelper.executeServiceCall( userService.saveUser( user ), handleSaveUserResult );
        }


    c) Execute the Service call using service helper, pass it a result handler.

            serviceHelper.executeServiceCall( userService.saveUser( user ), handleSaveUserResult );
   



4) View - makes use of the model and dispatches custom events that do CRUD on the Model. To achieve this as part of the View definition file, we ask Swiz to inject the model, and we bind model to UI fields


        [Bindable]
        [Inject( source="userController.currentUser", bind="true" )]
        /**
           * We could inject the whole controller instance, but we only need
          * one property from the controller, the current user, so we just
          * inject that property.
          */
   


        /**
          * Handle the user hitting the save button. We capture the form data
          * and dispatch a standard Flex event. No Swiz-specific events or
          * special central dispatcher needed!
          */

        private function saveUser() : void
        {
            user.firstName = firstName.text;
            user.lastName = lastName.text;
            user.email = email.text;
            var event : UserEvent = new UserEvent( UserEvent.SAVE_USER_REQUESTED );
            event.user = user;
            dispatchEvent( event );
        }

5) Model [is a Place holder for application data]- is a Standard Bindable class


        [Bindable]
        public class User
        {
            public var id : int;
            public var firstName : String;
            public var lastName : String;
            public var email : String;       
        }


6) Event Class [extends standard flash.events.Event class] - we need to ensure that event bubbles up by passing the bubble parameter value as true in the call to super()


    public class UserEvent extends Event
    {
        public static const SAVE_USER_REQUESTED : String = "saveUser";
        public var user : User;
       
        /**
         * This is just a normal Flex event. The only thing to note is that we set 'bubbles' to true,
         * so that the event will bubble up the display list, allowing Swiz to listen for your events.
         */
        public function UserEvent( type:String )
        {
            super( type, true );
        }
    }

7) Service Class [handles the remote communication]


    Declare public methods which make Service.send() call and return asyncToken object returned by the send() call. 



The standard ServiceHelper can attach the Result Handler & Fault Handler to this async token which will be invoked when the response is available.


This is how the service class is typically used in Controller. The serviceHelper object below is standard swiz Framework Service Helper


            serviceHelper.executeServiceCall( userService.saveUser( user ), handleSaveUserResult, handleSaveUserFault ); 

No comments:

Post a Comment