I think I might try a Swiz
After talking to a friend about frameworks and so forth. He mentioned the Swiz framework after incorporating it in a project he was currently working on. Being a dedicated Cairngorm guy I couldn't say that I had looked at Swiz before but from what I had heard it sounded like something that may be useful in the future. I had also thought it would make a great name for a cocktail "Excuse me bartender fetch me another Swiz"...
In a nutshell it's an IOC (Inversion of Control) Framework that utilizes Dependency Injection to wire your applications components and services together with your application views.
Your required application components and services are declared inside a Bean mxml class and is then instantiated on the pre-initialisation of the application.
There are some nice features that really caught my attention with this framework which I thought I would discuss.
-
CommandChain - is a class that lets you chain together your commands that can be run either in parallel or in sequence. This can be really handy for retrieving data needs to be fetched using multiple services/calls. You can specify a completeHandler which will be called once the chain is complete which can be extremely handy when preloading screens based on service calls and not data size.
eg.
public function getAdminDetails():void{
/* create a new CommandChain object */
var chain : CommandChain = new CommandChain(CommandChain.PARALLEL);
/* use AbstractController's createCommand function to build a DynamicCommand object for each
delegate call and add each to the command chain */
chain.addCommand(createCommand(projectDelegate.getProjectTypes, , projectType_results, local_Fault))
.addCommand(createCommand(projectDelegate.getProjectPriorities, null, projectPriorites_results, local_Fault));
.addCommand(createCommand(clientDelegate.getClientTypes, null, clientType_results, local_Fault))
/* now set a complete handler and call proceed to execute all the commands */
chain.completeHandler = adminCompleteHandler;
chain.proceed();
}
private function adminCompleteHandler() : void {
/* Broadcast a Swiz event to any view that's listening */
Swiz.dispatch("adminChainCompleteEvent");
} -
Dynamic Mediators - This concept removes the need to create an adequate amount of repetitive code when dealing with dispatching events and handling those events inside your controllers to pass onto the relevant services.
example without using a Dynamic Mediator
Child ProjectDetails View
private function saveProject() : void {
var event : SaveProjectEvent = new SaveProjectEvent(projectVO);
Swiz.dispatchEvent(event);
close();
}Main Project Dashboard View
private function onCreationComplete() : void {
Swiz.addEventListener("saveProjectEvent", onSaveProject);
}
/* handler to call the controllser method */
private function onSaveProject( event : DynamicEvent ) : void {
projectController.save(event.project);
}Project Controller.as
public class ProjectController extends AbstractController
{
public static const EVENT_SAVE_PROJECT : String = "saveProjectEvent";
[Autowire(bean="projectService")]
public var projectService : RemoteObject;
public function ProjectController() { }
private function saveProject(projectVO : ProjectVO) : void {
executeServiceCall(projectService.save(projectVO), saveProject_results);
}
etc...
}Now with the Dynamic Mediators we can cut out the logic that listens for an event and assigns the events property for it to be passed to the service. Like so.
This is as per the previous example Child ProjectDetails View
private function saveProject() : void {
var event : SaveProjectEvent = new SaveProjectEvent(projectVO);
Swiz.dispatchEvent(event);
close();
}All we need to do now is to add the Mediate meta tag with the required attributes to the saveProject method and it will create the listener and assign the properties from the event like so.
public class ProjectController extends AbstractController
{
public static const EVENT_SAVE_PROJECT : String = "saveProjectEvent";
[Autowire(bean="projectService")]
public var projectService : RemoteObject;
public function ProjectController() { }
[Mediate(event="SaveProjectEvent", properties="project")]
private function saveProject(projectVO : ProjectVO) : void {
executeServiceCall(projectService.save(projectVO), saveProject_results);
}
}How neat is that!
Of course the SaveProjectEvent would look something like this.
public class SaveProjectEvent extends Event
{
public static const EVENT_SAVE_PROJECT: String = "saveProjectEvent";
public var project : ProjectVO;
public function SaveProjectEvent(project : ProjectVO)
{
super(EVENT_SAVE_PROJECT);
this.project = project;
}
}
Where to go from here?
Chris Scott has done a great job in releasing this along with some easy to follow documentation and examples. Download the framework here
Christophe Coenraets has also a nice article about swiz here with a sample application using Blaze ds.
It'll be interesting to test the performance of swiz on some larger sized applications.
