Sample application using the Swiz Framework and BlazeDS

Sample application using the Swiz Framework and BlazeDS

There have been a lot of discussions around Flex Frameworks lately. Tony Hillerson has an interesting series here: 1 2 3 4 5 6

A relative newcomer on the list is Swiz, the work of Chris Scott. I figured I would give it a try, and create a Swiz version of the inSync application that I often use to try out and demonstrate different features and techniques in Flex and Adobe AIR. This is not an endorsement of Swiz over other frameworks. I simply wanted to share the sample application I built as part of my own research as a (neutral like a Swiss) Evangelist.

View the source code.

Application Highlights:

At his core, Swiz is a simple inversion of control framework. Using an IoC framework, the components of your application (for example, Views) don’t instantiate or even look up their dependencies (the objects they work with). The framework injects those dependencies when the components are created (hence the term “Dependency Injection” also used to describe this approach). The result is looser coupling and more reusable components. The components managed by the Swiz IoC framework are called beans.

1. Defining your beans

The beans of the inSync applications are defined in Beans.mxml:

<BeanLoader xmlns="org.swizframework.util.*" xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:controllers="insync.controllers.*">

    <DynamicChannelSet id="myAmfChannel">
		<serverPort>8400</serverPort>
		<contextRoot>/lcds-samples</contextRoot>
    </DynamicChannelSet>

    <mx:RemoteObject id="contactService" destination="contacts" channelSet="{myAmfChannel}"/>

    <controllers:ContactController id="contactController"/>

</BeanLoader>

2. Bootstrapping Swiz

You bootstrap Swiz in the preinitialize event of the application. You can have multiple Beans configuration classes. You pass your configuration classes in an array as an argument of the loadBeans function. In inSync, we have only one configuration class (Beans.mxml).

Swiz.loadBeans( [Beans] );

3. Injecting dependencies

Swiz uses the [Autowire] custom metadata to inject beans into other beans and views.
In inSync, the contactService RemoteObject is injected into ContactController.as as follows:

[Autowire(bean="contactService")]
public var contactService:RemoteObject;

… and contactController is injected in the Views. For example, in ContactForm.mxml:

[Autowire(bean="contactController")]
public var controller:IContactController;

Because we use an interface, the View is not coupled to a specific implementation of the controller. If you don’t like to have a reference to the controller in your view, keep reading…

4. Dynamic Mediators

Swiz is an IoC framework. Beyond that, it doesn’t force you to use any specific design pattern. So, if you don’t like to have a controller in your view, you can, for example, set up the view to dispatch events instead. For example, in the save() function of ContactForm.mxml, you would replace:

controller.save(contact);

with:

Swiz.dispatchEvent(ContactEvent.SAVE, contact);

You could then listen to this event in some “mediator” class using Swiz.addEventListener(ContactEvent.UPDATE, someHandler), but the dynamic mediator feature of Swiz allows you to automate this process. For example, you could annotate the save() function of the ContactController class as follows:

[Mediate(event="ContactEvent.SAVE", properties="contact")]
public function save(contact:Contact):void
{
	executeServiceCall(contactService.save(contact), save_result, null, [contact]);
}

Swiz will automatically set up an event listener, invoke the annotated method when the event is dispatched, and pass the specified event properties (in this case: “contact”) as the function parameters.

5. Prototypes

By default, components defined in your Beans configuration classes are singletons. In other words, the same instance of a Bean gets injected everytime it is autowired. In some situations, you may want to inject a new instance of a Bean everytime it is autowired. For example, if you open multiple instances of the same View, you may decide you need one controller per view. Prototypes allow you to define non-singleton Beans in your Beans configuration class using the following syntax:

More on prototypes here.

<factory:Prototype xmlns:factory="org.swizframework.factory.*"
        id="contactController"
        className="insync.controllers.ContactController"/>

Installation instructions:

  1. Install the BlazeDS turnkey server. (To be clear: you don’t have to use BlazeDS to use Swiz… That’s just the sample I wanted to build.)
  2. Download insync-swiz.zip, and unzip it on your local file system.
  3. Copy insync-swiz/java/classes/insync to blazeds/tomcat/webapps/samples/WEB-INF/classes/insync.
  4. Add the following destination to blazeds/tomcat/webapps/samples/WEB-INF/flex/remoting-config.xml:
  5. <destination id="contacts">
            <properties>
                <source>insync.dao.ContactDAO</source>
                <scope>application</scope>
            </properties>
    </destination>
    

  6. Copy insync-swiz/sampledb/insync to blazeds/sampledb/insync
  7. Edit server.properties in blazeds/sampledb, and modify the file as follows to add the insync database to the startup procedure.

    server.database.0=file:flexdemodb/flexdemodb
    server.dbname.0=flexdemodb
    server.database.1=file:insync/insync
    server.dbname.1=insync
    server.port=9002
    server.silent=true
    server.trace=false
    

  8. Start the database (startdb.bat or startdb.sh)
  9. Start BlazeDS
  10. In Flex Builder, create a new Flex project called insync-swiz. You don’t have to select any “Application server type”.
  11. Copy the Swiz swc and the flexlib swc (used for the SuperTabNavigator) from insync-swiz/flex/lib to the lib directory of your project
  12. Copy the files and folders from insync-swiz/flex/src to the src directory of your project
  13. Open Beans.mxml and make sure the serverPort and contextRoot properties in DynamicChannelSet match your server setup.
  14. Run the application

Summary

Swiz was easy and enjoyable to work with. It is also non intrusive and definitely introduces some interesting ideas. A few lingering questions I didn’t have time to investigate…

  • Swiz relies on introspection (describeType) to find its custom metadata in the objects added to the display list (with the exception of classes in the flash.* and mx.* packages). This has a performance impact. I’m not exactly sure how much of an impact it would have in real life apps yet…
  • How well does Swiz work with Modules or Marshall Plan subapplications?

If you are looking for more information, check out the following resources:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • LinkedIn
  • StumbleUpon
  • Twitter
This entry was posted in Air, BlazeDS, Flex, Java, LCDS. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

9 Comments

  1. Posted February 13, 2009 at 5:30 pm | Permalink

    Hi Christophe,
    cool article and nice explanation of Swiz!
    One thing I would like to comment is:
    [Autowire(bean="contactController")]
    public var controller:IContactController;

    You could leave out the bean name if there exists only one instance in your beans of that type aka autowire-by-type and it would read:
    [Autowire]
    public var controller:IContactController;

    And thanks for the link!
    Brian Kotek also did a blog series about Swiz which is pretty cool: http://www.briankotek.com/blog

  2. Posted February 18, 2009 at 1:29 am | Permalink

    Nice Framework!!!!! Very Very intersting!! since Spring uses IoC right?

    Cheers!

  3. Lawrence
    Posted July 16, 2009 at 10:02 pm | Permalink

    I have tried Swiz but so far have issues and after a day, not sure I’ll continue but I love the concept. Can anyone explain why my service would always be null when loaded in Beans.mxml?

    I use Grails on server side and when I have in my mxml file the same RemoteObject definition I can access the service properly and it is not null like it is when using Swiz.

    Any help appreciated.

  4. Lawrence
    Posted July 16, 2009 at 10:04 pm | Permalink

    Somehow this didn’t make it into my post….this is my service in either Swiz Beans.mxml or in my main app mxml.

  5. Lawrence
    Posted July 16, 2009 at 10:04 pm | Permalink
  6. Lawrence
    Posted July 16, 2009 at 10:05 pm | Permalink

    Ok, I give up :) – my code is never included in a comment here!

  7. Ansury
    Posted August 1, 2009 at 2:59 pm | Permalink

    Lawrence, I can’t see your example but clearly you’re missing something. :) Follow the instructions on the Swiz framework distribution page, if you do everything there it will work. After working with it a few more days it should become pretty obvious what to check for.
    http://code.google.com/p/swizframework/

  8. Pranay
    Posted January 28, 2010 at 1:22 pm | Permalink

    I used Parsley (http://www.spicefactory.org/parsley/ )framework in my last project. Its very light weight, easy configurations, annotations, complete seperations of reusable components.
    I liked that.

  9. Amit
    Posted June 29, 2010 at 12:54 am | Permalink

    Your articles are always nice and developer friendly :)
    We are going start a project in which we will be using Flex+BlazeDS+Spring. We are using Spring BlazeDS integration for the same.

    But some how our team does not like the tight coupling of calling spring pojo method from Flex UI.
    Can we use any controller in between?
    Or can you suggest us an alternative to avoid this Tight coupling?

4 Trackbacks

  1. By Shared Items - March 1, 2009 | hufkens.net on March 1, 2009 at 2:16 pm

    [...] Sample application using the Swiz Framework and BlazeDS [...]

  2. [...] If you already installed the Swiz version of inSync, you can skip steps 1, 3, 4, 5 and [...]

  3. [...] my recent explorations of “Swiz”, and “Spring ActionScript” (1,2,3), I decided to take the new version of the Parsley [...]

  4. [...] Christophe Coenraets – uma aplicação simples usando o Swiz Framework e BlazeDS Exemplo bem interessante, onde um ponto a ser observado foi como foi definido o acesso ao RemoteObject através do Swiz. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>