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:

23 Responses to Sample application using the Swiz Framework and BlazeDS

  1. Sönke Rohde February 13, 2009 at 5:30 pm #

    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. Israel Gaytan February 18, 2009 at 1:29 am #

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

    Cheers!

  3. Lawrence July 16, 2009 at 10:02 pm #

    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 July 16, 2009 at 10:04 pm #

    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 July 16, 2009 at 10:04 pm #
  6. Lawrence July 16, 2009 at 10:05 pm #

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

  7. Ansury August 1, 2009 at 2:59 pm #

    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 January 28, 2010 at 1:22 pm #

    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 June 29, 2010 at 12:54 am #

    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?

  10. porno September 24, 2010 at 12:28 pm #

    en deli porno videolari tikla hemen izlemeye basla.

  11. jogos de motos October 22, 2011 at 3:36 pm #

    The Adobe AIR runtime permits developers use the Flex framework to build rich web applications that run as standalone client applications without the constraints of the browser. Adobe AIR applications run across desktop operating systems and on Android, BlackBerry® Tablet OS, and iOS mobile operating systems. With AIR, Flex developers can use their existing skills and code to build highly engaging, visually rich applications that merge the power of local resources and knowledge with the reach of the net.

  12. mensagens de carinho October 22, 2011 at 3:39 pm #

    Please find attached my presentation about Swiz from yesterday. As I already said, I am liking Swiz and I will definitively use it in my projects where I think its appropriate. Not only because I am Swiss ;) , but also because it addresses the main things I miss in Flex like IoC, sometimes dont like in PureMVC like boilerplate code or built over and over again like the dynamic channels.

  13. roof structure November 22, 2012 at 3:21 pm #

    I really enjoy looking at about this site, it contains superb posts.

  14. Marietta May 24, 2013 at 7:49 am #

    Hi i am kavin, its my first occasion to commenting anyplace, when i read this piece of writing i thought i could also make comment due to this good paragraph.

  15. Asking questions are really pleasant thing if you are not understanding anything completely, however this
    article gives nice understanding even.

  16. compro oro June 16, 2014 at 1:17 am #

    Wow, incredible blog layout! How long have you been blogging for?
    you made blogging look easy. The overall look of your site is magnificent,
    as well as the content!

  17. Foster Braun June 21, 2014 at 12:29 am #

    Hi there! This post couldn

Trackbacks/Pingbacks

  1. Shared Items - March 1, 2009 | hufkens.net - March 1, 2009

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

  2. Christophe Coenraets » Blog Archive » The “Spring ActionScript” Framework – Part 2: Autowiring - March 12, 2009

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

  3. Christophe Coenraets » Blog Archive » Building a Flex Application with the Parsley Framework - July 16, 2009

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

  4. [Flex & AIR] Swiz Framework – meus primeiros passos | Erko Bridee - July 13, 2010

    […] 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. […]

  5. Felipe Borella » Swiz Framework - May 9, 2011

    […] Simples aplicação usando Swiz e BlazeDS Código para download aqui […]

  6. Mxml samples | Eyesoftheeast - March 6, 2012

    […] Sample application using the Swiz Framework and BlazeDSFlex Custom mxml tags example. Posted on: September 9, 2008 at 12:00 AM. MXML files which functionalities are extended in to other mxml files are called … […]

Leave a Reply

css.php