Swiz Framework – Brutally Simple

After the 360Flex took place in San Jose I watched a few of the video recordings which are available through Adobe Media Player. Ted Patrick was also so nice to put them on his blog.

I am always interested in new Flex frameworks and Swiz got my attention. I watched the video and started to play around with it. What I like most about the framework is the use of metadata aka annotations which really reduces the amount of code.

When I first checked out the source I recognized that the DynamicMediator which was shown in the presentation was not committed yet. However, I started to build an example project where the views got the controller injected via Autowire. After a few days I was a bit frustrated because I also wanted to use the Mediate metadata and surprise, the code was committed. I updated the library and could remove all the controller references from the views which decoupled things like I wanted it to have. And recently Chris Scott, the developer behind Swiz, also blogged about the update for DynamicMediator.

When you have no clue what I am talking about be sure to check the presentation of Chris at 360Flex. There was no more need to study the docs after watching the video since the framework is really a “Brutally simple micro-architecture for Rich Internet Application development with Adobe Flex“.

So what’s it all about? Swiz is an IoC or Dependency Injection framework. I have used IoC frameworks in the past and got the point why it is cool but I always disliked the XML configuration files. Swiz uses similar concepts like Spring in the Java world were annotations were introduced in 2.5.

Swiz currently support two kinds of annotations: Autowire and Mediate
Autowire automatically injects references to objects which very defined in the BeanLoader and mediated methods get invoked when the belonging event was fire with event member variables as parameters.

The other hot discussed IoC framework is currently Mate. The presentation of Laura Arguello at 360Flex is really great and really nails down what the common problems are and how to solve them. I pretty much share the dislike of coding in XML blogged by Olesen and discussed on InfoQ. Another review of Mate is done by Flash Magazine and the structure is visualized in a diagram by Fatara Systems. But don’t get me wrong. Mate looks really awesome but to my personal preference I prefer something more lightweight like Swiz.

The weakness of Swiz is currently that the Autowire and Mediate uses strings instead of constants which means there is no compile time check if things work or not. On the one hand you should use conventions how you name the “beans” to autowire and the event types to mediate and on the other hand it would not be to hard to build a kind of pre-compiler which validates that the annotations match. My convention for beans is to use the class name starting lowercase like

<Beanloader ... xmlns:ctrl="com.soenkerohde.myproject.ctrl.*">
    <ctrl:MyController id="myController" />
</Beanloader>

.
So it can be used/injected with:

[Autowire(bean="myController")]
public var myController:MyController;

For the dynamic mediators my current approach is to type the event with the full qualified classname like

[Mediate(event="com.soenkerohde.myproject.events.LoginEvent", properties="username,password")]
public function login(username:String, password:String):void

The event would look like this:

package com.soenkerohde.myproject.event
import flash.events.Event;
public class LoginEvent extends Event
{
  public static const LOGIN_EVENT:String = "com.soenkerohde.myproject.events.LoginEvent";
  public var username:String;
  public var password:String;
  public function LoginEvent(username:String, password:String)
  {
    super(LOGIN_EVENT);
    this.username = username;
    this.password = password;
  }
}

However, it would be nice to have something during compile time. Let’s see how Swiz evolves and what other frameworks enter the arena.

8 comments to Swiz Framework – Brutally Simple