The Flex Application Frameworks SmackDown (sorta)

February 27th, 2009 Via Tsuji, Consultant  (email the author)

This entry is part 4 of 6 in the series Flex Development

The Selecting the Right Flex Application Framework post provided a summary comparison of the Flex application frameworks out there, highlighting the ones I thought were the “heavyweights” a Flex developer should focus on. The purpose was primarily to help you make a quick, easy, but well-informed decision, without looking at the code.

But many of us wish to look under the covers and see a detailed comparison of the frameworks. So I took Adobe’s Flex Builder 3 Getting Started tutorial example of a simple Flickr photo search and re-implemented it in Cairngorm, PureMVC and Mate.

No Framework

But before we discuss each framework, let’s quickly go over the problems that the frameworks are trying to solve. All Flex application frameworks have been created, because there are problems with implementing a Flex application without one (or, of course, with another framework).

If you take a look at the Code Files in the tutorial, you’ll see that the application doesn’t really need a framework, because it’s simple and straightforward. However, oftentimes, applications are just the opposite. So implementing a complex Flex application, without a framework and following the model described in the tutorial, will require the following:

  • Separation of Concerns. Of course, the major rationale for using the framework is to separate the code based on its roles/responsibilities, i.e. model, view, controller. The advantages of which I won’t discuss further. Read Wikipedia. In any case, without a framework, the data, the business logic and the interface are mixed inside the FlickrRIA application.
  • Data Sharing. Data, e.g. the photoFeed, needs to be shared to other screens in the application. It will not be best practice to expose the variables in different pages for other pages to access. Think spaghetti, but not for eating.
  • Service Sharing. The HTTP Service and the like, e.g. the photoService, needs to be reused in other functions of the application, as well as the logic for using that service, e.g. the requestPhotos() method. Similar dilemma as with data sharing.
  • Unit Testing. Testing must be easy to do. Automation is highly recommended. It can still be done without a framework, but it is difficult with the logic embedded in the Flex components.

Cairngorm

For all 3 frameworks, the development process starts with implementing the Photo value object. Beyond that, the frameworks differ.

Flickr Example Using Cairnorm (Flex project archive, ZIP)

Cairngorm addresses the difficulties in the absence of a framework with recommended best practices and the framework. Ease of testing and maintenance are derived from the separation of concerns.

  • Model. Data is encapsulated and shared via the singleton ModelLocator, and is bound to the Flex components of the application.
  • View. The View is made up of different visual Flex components in the application. It has two jobs:  1) dispatching events to request data or invoke actions on the backend and 2) watching for Model updates and updating the interface as needed.
  • Controller. The Controller is implemented via the FrontController. It manages which command is invoked when an event is dispatched from the View.
    • Business logic is encapsulated and spread out in Commands. It invokes the service (via delegates), handles the results, and updates the Model.
    • Delegates add an extra layer of indirection between the application (client) and the server. They locate the service, invoke it and pass the necessary parameters.
    • Services are encapsulated and shared via the singleton ServiceLocator.

I found Cairngorm really easy to learn and work with. With its wide support and defacto standard status, it almost always is the chosen framework. It’s good for medium to large development teams of varying skill levels in Flex and ActionScript.

However, there are a few disadvantages with Cairngorm.

  • The ModelLocator can get huge and unwieldy; depending on how large your application is and how much data it keeps. To resolve this, I have seen using more than one ModelLocator to separate, say, the business data from the presentation data. But it still won’t be fun to handle merge conflicts with everyone updating it, if you’re in a large team.
  • Extending the CairngormEvent for each event to encapsulate the event name ID and data was not too pleasant. I would imagine it’s easy to end up with duplicate event IDs in a large application (which is why a naming convention would be vital). I suppose you can create a generic Cairngorm event, but the data parameters wouldn’t be type-checked.
  • Lastly, I wasn’t in favor of referencing the ModelLocator in all of my Flex components. I favored passing a data reference from a parent component to keep my view components as reusable and un-Cairngorm-ish as possible.

PureMVC

Flickr Example Using PureMVC (Flex project archive, ZIP)

PureMVC addresses the difficulties similar to Cairngorm with a few more layers of indirection.

  • Model. Data is encapsulated and spread out in Proxies. These are referenced singletons. The Model handles interaction to the data access layer in persisting or retrieving data. In the code, I used the Proxy and Delegate pattern. No huge model locators found here!
  • View. The View is implemented with Flex components and Mediators. The mediators run interference between the view components and the rest of the application. The View listens to events, sends notifications to request data or invoke an action, and receives notifications to update the visual components. The view components do not have any PureMVC-specific code and are reusable.
    • Note that events do not need to be customized. The mediator retrieves the necessary data from the view component and passes it to the command.
    • Events are, in effect, replaced by notifications — similar to a Flex event. Of course, there is no type-checking for the parameters.
  • Controller. The Controller is hidden from the developer, but maintained the mappings between notifications and commands.
    • Commands, like in Cairngorm, contain the business logic. But for data persistence and/or retrieval, they defer to the Proxies.
    • Unique to PureMVC is the Façade. It is a singleton that initializes the Model, View and Controller components. All components can be accessed through the façade, as well as all notification and/or event IDs.

PureMVC resolves a lot of the aspects I didn’t like about Cairngorm. It is good for complex applications, with a lot of common components, e.g. forms-based applications with workflows. And it works well for medium to large development teams.

However, it does have a few disadvantages:

  • PureMVC cannot rely on data binding innately. Manually updating the data on the view from the mediator could get annoying in a larger application. I suppose that is the “sacrifice” that must be made in making the view components purely portable, without any knowledge of PureMVC. However, because of this, I began to question, how often do I need it to be portable? Apart from this exercise?
  • It’s more complex than other frameworks. Therefore, it does incur a steeper learning curve than others. It may not be too difficult for a developer who knows Java, for example. But complexity and flexibility can breed more architectural mistakes.

Mate

Flickr Example Using Mate (Flex project archive, ZIP)

Mate responds to the challenges of having no Flex application framework in a simple, “indirect” way. It is a “tag-based, event-driven Flex framework”, but can facilitate an MVC implementation.

  • Model. Mate doesn’t provide specific model components, but enables the data sharing and encapsulation with the use of cached generators. In examples, they call these managers.
  • View. The View is implemented with Flex components, which handle event dispatching for data retrieval and/or method invocation. Similar to PureMVC, the view components are highly reusable, since they don’t have knowledge of Mate.
  • Controller. The Controller is configured via the event map, the core of Mate.
    • Event handlers are defined in the map to specify what needs to happen when certain events are dispatched. It can support a variety of handlers, such as HTTP service invokers, web service invokers, command invokers and method invokers. In combination, these comprise the business logic triggered by the dispatched event.
    • Instead of binding view properties directly to the Model, Mate uses injectors. Injectors are also defined in the event map to set properties from a source (in the Model) to a target (in the View). Similar to event handlers, injectors observe changes from the Model to trigger updating the View.

Mate also resolves a lot of my issues with Cairngorm, but is simpler overall than PureMVC. It is still unable to take advantage of data binding, but I prefer injectors to setting data through code.

Mate would work well for complex applications and is suited for small to large development teams. And unlike other IoC Flex frameworks, it provides more meat behind the event handlers.

Note: The embedded code files have comments that contain more details about each ActionScript class/component. It explains what needs to be created/updated for new functionality and what are the base objects. I have also included a “simplistic” sequence diagram (well, I tried as best I could to make it simple) of the event flows for each framework, when a photo search is performed.

Entry Filed under: Agile and Development

10 Comments Add your own

  • 1. wjptak  |  March 3rd, 2009 at 3:35 am

    Hi Via,

    Thank for your article. Your comparison is well prepared, however I might add few things.
    1. ‘No framework’ almost always end up as a sort of framework – all of us uses some approach that we shape up and copy from one project to another. On the other hand, it is very important to stress that this approach requires quite high skills from developers when they share the project. This is major disadvantage.
    2. As one of major disadvantages of Cairngorm and PureMVC I would also indicate big load of files that are needed to be created. Also, PureMVC as one of good practice is using code behind, which is suitable for many developers but result in even more files. This doesn’t happen in frameworks like Mate or Swiz.
    3. Cairngorm and Modules – there is a lot of buzz about this but surely at the moment we don’t have a very clean approach to do this. Of course it is possible, however it’s not supported by the framework by default.
    4. Mate – for many developers problem here lies in using only MXML files, sometimes this approach isn’t so very handy.
    5. Swiz – I believe it’s worth mentioning. Supported by the large and dynamic community, shaped and polished each week. It has great advantages, I have found it really useful and it allows me to write code at much higher speed than other ones. However, it has also disadvantages – for example, refactoring might be difficult due to use of meta tags.

    Best regards,
    W.

  • 2. John Blanco  |  March 3rd, 2009 at 9:27 am

    One advantage to PureMVC’s disadvantage is that I think, in terms of the Flex implementation, data binding isn’t terribly efficient with anonymous bindings (and large models). By making you not use the binding, it has the effect of making our apps a lot more efficient, especially when rapid updates to data are occurring.

    But, it’s a bad tradeoff, because we’re giving it away for free, and a lot of apps don’t have binding inefficiency problems.

  • 3. cease  |  March 3rd, 2009 at 2:38 pm

    i think another advantage of puremvc is the extensibility of it. The plugins or addons developed for this framework by the community and founder are pretty awesome. I think the flexibility of the mediator class and manually setting vs the inflexibility of databinding is worth it

  • 4. iongion  |  March 3rd, 2009 at 3:16 pm

    Without transforming this in a rant:

    1) PureMVC – this is so painful to understand, probably it has the best documentation compared to other frameworks (and not only flex frameworks), but the darn thing is really hard to grasp.

    2) Mate – it is innovative, very mxml-ish (very cfml-ish :) ) … but what your code ends up to look is like a tag soup, some would like the soup, other will not see it beautiful.

    3) Cairngorm – EASY, it is the easiest to learn of them all, especially for flash developers moving to flex with no java experience(i learned design patterns using this framework) … but it also introduces an “anti”-pattern, that model locator, that you so diplomatically say it gets huge (it gets gargantuan for minimal applications)

    To conclude, if one wants to work from a non-framework with a framework and wants to learn to use one fast, a good choice would be cairngorm.

  • 5. James Law  |  March 3rd, 2009 at 4:41 pm

    nice summary, thanks for sharing. I think swix is promising as it strikes a nice balance and uses DI, although I haven’t put it to use yet. I find mate to be reasonably easy to learn, and if you don’t go overboard in using everyone of their possible xml tags, then its pretty good. I find using it to manage commands, events, and mediators to be all I need, coding everything else in AS3.

    Anyone tried the spicefactory stuff?

    Thanks

  • 6. Darren  |  March 3rd, 2009 at 5:50 pm

    @ Via, there is nothing about the Cairngorm framework that prescribes that you reference the ModelLocator in all your components (except some of the examples do this). You should only reference the ModelLocator in your top level components/modules and inject your data into sub-components. It’s very easy to implement your own DI in Cairngorm, for example, these work in Cairngorm:

    http://www.ericfeminella.com/blog/2008/09/21/dependency-injection-iocdi-in-flex/
    http://www.rzrsharp.net/2009/02/roll-your-own-dependency-injection/

    @ wjptak, Cairgorm 2.2.1 was released in May 2007 with support for modules. All of our Cairngorm projects use modules where I work. What specific issues are you referring to?

  • 7. Via Bulatao  |  March 3rd, 2009 at 8:57 pm

    @wjptak: I agree with your comment on having “No Framework” to some extent to some degree. A framework may form if somebody does take the lead to form it. With regards to Cairngorm and PureMVC having a lot of setup, it’s true there will be a lot of files required upfront, but I don’t think it’s necessarily a major disadvantage, since it’s a one-time effort, unless you’re developing a prototype. Point taken with regards to Swiz.

    @John and cease, I agree with your comments on PureMVC. But what my posts have been really driving at is that it depends on your application, your team, your organization, etc. I don’t agree that PureMVC (or any of the other frameworks) will be the right choice all the time. But if you need extensibility, yes, PureMVC is a good candidate.

    @iongion, I also agree with your comments on the frameworks, but for me, I don’t think it’ll always be Cairngorm.

    @James, hmm … I took a look at Swiz, but just like you said it’s a balance. I figured I’d put the framework with the most and least number of required artifacts. Swiz is somewhere in the middle. But I may have missed some of its intricacies, by not including it here.

    @Darren, I agree with your comment on the ModelLocator. And in my previous post, I do know the solution to that. But not everyone agrees with me that it should just be at the top-level. And since that’s how the Cairngorm tutorial teaches it, that’s how I presented it here.

    Thanks for all your comments!!

  • 8. bong  |  January 29th, 2010 at 5:27 pm

    Good articles it helps a lot .. are you pinay?

  • 9. Karl Sigiscar  |  March 17th, 2010 at 7:19 am

    Concerning Cairngorm:

    Adobe Consulting recommends to NOT store any data at the ModelLocator level. Instead, you should create model objects that are referenced from the ModelLocator. The ModelLocator exists to LOCATE your model objects.

  • 10. Karl Sigiscar  |  March 17th, 2010 at 7:21 am

    … just as the ServiceLocator exists to locate your Service components.

Leave a Comment

Required

Required, hidden


7 + two =

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

© 2010-2012 Summa All Rights Reserved -- Copyright notice by Blog Copyright