The Blog

Feb 10, 2008

Finally - A Lightweight and Simple Flex Framework? 

by Maxim Porges @ 7:22 PM | Link | Feedback (2)

I've been messing around with dynamic classes in Flex today, and I've found some pretty neat ways to do things.

I got in to this while writing some code for my session at cf.objective() this year. I wanted a non-trivial (yet simple) example of how to just use Flex's built-ins to make a maintainable application, and so I started to roll some code.

A key part of my philosophy behind Flex apps is that they should be as easy to create and configure as I find Spring apps to be in Java. You should be able to combine a simple service/data/functionality "locator" object with regular old tried-and-trusted OO design patterns, and be able to end up with something simple that is exactly what you need. So, to start with, I created a basic "locator" object to enable any part of the application to find, bind to, and/or invoke other objects (usually Singletons) within the application. I got the basic app working and tested, and was ready to move on.

With this complete, an old desire within me was stirred up to be able to mix the XML syntax of MXML with the scriptability of ActionScript 3 to create a simple IoC-like object factory/locator for Flex apps. The ideal solution would allow you to do Spring IoC-like configuration in MXML, without having to use a bunch of special classes or write additional glue-code yourself. I always find I have to write two or three classes to abstract Spring when I use it in our Java services at work, which isn't a huge chore, but is totally unnecessary in Flex due to how cool the concept of MXML is.

This isn't the first time I've tried doing this, but I've always failed to find a good solution. I've failed mainly because I've relied on alpha frameworks from other people rather than just doing it myself. I downloaded something called Prana a while ago (couldn't get it working at the time as I recall [probably my fault] and it didn't leverage MXML like I want to), and while MG:Flex looked promising it was still going in a direction that looked more boilerplate-esque than I wanted. That being said, my stipulations were tight: I was looking for a zero-boilerplate solution.

Since Dan and I were looking at some cool stuff on the FlexOnRails site during the acts_as_conference, I had it fresh in my mind that the capabilities of dynamic classes and proxies in AS3 might be the key to what I wanted. After thinking about it a while, I did some Googling, and found an example of almost exactly what I had in mind. I modified the code slightly to add singleton-style behavior, did some experimenting, and voila - exactly what I wanted!

I'm going to share what I wrote with a few people I respect before I show it to the world, but I think I may have finally cobbled together the Flex framework I always wanted. At present, it consists of a whopping one class, which you don't even extend.

It's pretty hot. Since you configure everything in MXML, you can basically wire up your components to each other and declare event listeners and anything else you want to do all in one place. You can alternatively create multiple containers to isolate specific parts of app functionality/responsibility as you see fit, or bundle it all together in one file if you prefer - your choice. Adding access to the framework in your own files is as easy as writing a single tag.

Watch this space if you're interested. I hope to have something out on the web in a week or two once I've got it all vetted with my peers.