Archive for January, 2009

Choosing a Flex Architectural Framework

Friday, January 30th, 2009

If you’re working on a moderate to complex Flex/Air application I hope you are using some kind of MVC style framework to help with organizing your components and the interactions between them. If you do not use a framework, you will unknowingly be creating your own. This, consequently, makes it harder for others to get up to speed with any maintenance that will need to be completed, and you will be on the hook to create all the related documentation. Also, why re-invent the wheel when pre-existing frameworks, such as Cairngorm, PureMVC, Swiz, or Mate, already implement best practices and are field-tested? All you need to do is set aside a little time to learn them.

I’m currently working on a greenfield project where I have a Flex/AIR front-end talking to RESTful services provided by Groovy on Grails. I’m a few weeks into the project, but my latest challenge has been deciding on what MVC ActionScript/Flex framework to use. Let me attempt to tell you a little about my process thus far, in case you’re going through this yourself.

If you’ve done any Flex at all I’m sure you’ve heard about the market-leading framework Cairngorm. It’s currently the most popular choice for enterprise worthy applications. It took its inspirations from the Sun J2EE Blueprint Catalog and implemented all the Java based patterns in ActionScript (AS). Some of these well-known patterns are the Command, Front Controller, MVC, Singleton, Value Object, and the Delegate. I believe some reasons for the success of Cairngorm is because AS is very similar to Java with a little mix of JavaScript thrown in for good measure. Then, you add the fact that Adobe (and previously Macromedia) likes targeting server-side Java guys (LiveCycle Data Services anyone?), which should make them comfortable with Cairngorm once they get up to speed on Flex and AS. Since it’s been around for a long time there’s quite a bit of documentation on it (in blogs, as well as books), and even code generators, which attempt to speed up development time. In Flex Builder 4, which will be released later this year, Adobe will be integrating Cairngorm support into Flex Builder out of the box. Very appealing indeed!

I know PureMVC is getting a lot of hype lately as it enforces a strict MVC and has been ported to a lot of different platforms such as Java, AS, and even Objective C and Ruby. I haven’t used it, but I read up on it quite a bit. It seems a little awkward to me since it doesn’t take advantage of Flex binding. I know this is because it remains a language/platform independent framework that focuses on giving you a way to do MVC. I’m sure PureMVC has its merits though, and I believe I would enjoy it if a project I was working on already had it… same for Cairngorm. The only things I would look to add to each of those would be some good ole Inversion of Control Prana style (now called Spring ActionScript) to spice things up, and simplify some of the code.

The next framework I read up on was Swiz. This seems like a great combination of MVC and IOC, and it’s the only framework to use AS3 Metadata to wire up all your components. It is a very innovative approach with the only concern being maturity compared to Cairngorm and PureMVC. I probably would look into this one a bit more if it wasn’t for my first choice…

I started my project thinking that I’ll go with Cairngorm, but as you probably figured out, I changed my mind. This happened after I saw a demo of a Flex-based framework called Mate. In my opinion, Mate is one of the more lightweight frameworks out there. I say lightweight because it attempts to use native Flex events whereas in Cairngorm you are forced to have your events implement a Cairngorm interface. This is a dependency I do not care much for. Mate’s philosophy is to make your views oblivious to what is going on in the backend… from getting some data from your server to injecting those values into your model. You can have all your custom components be free of any references to any Mate related classes, which is a good sign for an MVC oriented framework. Mate has a concept that none of the other frameworks have that I find intriguing… maybe because its different from the J2EE inspired frameworks, and I am digging how it takes advantage of MXML, whereas the others use more ActionScript. Basically, the main concept of Mate is to define an Event Map, which is just a switchboard of what actions to invoke when a specific event is fired. This is where most of your server calls come into play and injecting the values into your Managers which are singleton based models for your views and also take advantage of bindings. This Event Map is all tag based, which I like, but since it’s all AS under the covers, you probably can implement your Event Map in a regular class if you prefer.

I’ve just begun really using Mate and I’m enjoying the process of wiring everything together. It seems well thought out and simple, but also elegant. I’m also enjoying the fact that I’m programming in more of an event-based language, which these frameworks promote, especially Mate. As I get further into my project I’ll try to give updates about how my experiences with Mate are transgressing.

To get a better comparison of these frameworks you should read the 5 part article “FrameworkQuest 2008“. It’s a great read seeing how Tony Hillerson went and developed the same Twitter application using these 4 different frameworks and discusses the benefits and weaknesses of each.

So go and find yourself a framework to use. I’m sure whichever one you pick will work out for the best… if not, well then there’s other options as you can see.

Bookmark and Share

Groovy Dates Are Groovy

Thursday, January 8th, 2009

I recently came across a new tidbit of info while working with the Date class in the GDK and I thought I would point it out real quick. I wanted a easy way to compare two Date objects while disregarding time. One to represent today’s date and the other to represent a future date. So I took a look at the GDK docs for the Date class and I noticed that since version 1.5.7, Groovys’ Date now has a static parse(..) method and a instance format(..) method which worked out nice for me in terms of writing less code. Here is a snippet of my code:

def myFormat = 'MM/dd/yyyy'
if Date.parse(myFormat, '02/03/2009') >= Date.parse(myFormat, new Date().format('MM/dd/yyyy')) {...}

If you did Date comparisons using regular Java, you would know that you couldn’t get away with something like this. I’m loving Groovy more and more everyday.

If you have an even shorter or more succinct way of coding this simple comparison, please share it.

Bookmark and Share

Chicago Groovy User Group

Tuesday, January 6th, 2009

I wanted to remind everyone that in one week from today, the Chicago Groovy User Group will have its inaugural meeting in the Chicago Loop. The featured speaker will be Sten Anderson from CITYTECH who will give a talk on using Groovy to create DSL’s. There will be free food and drinks available and a book raffle provided by CITYTECH. Please visit http://cgug.org for more information and to sign up for future notifications.

We have some cool ideas planned for upcoming meetings. Besides having nationally recognized speakers and authors stop by, we are also working on some informal code camps, as well as Lightning Talks where attendees will bring up their Grails related problem that we’ll discuss and solve as a group.

I am excited and looking forward to a great new year. And, what’s a better way than to start off by joining a new user group that’s focused on a great language and platform.

Bookmark and Share