Filed Under (ActionScript, Flex, RIA) by jonr on May-21-2007

I have been looking at the Cairngorm Flex Application Framework. Cairngorm is a MVC framework implemented in ActionScript 3.0 to support development in Flex.

As a Java developer, I have quite a bit of exposure to the MVC pattern, and I definitely want to have a healthy Separation of Concerns (SoC) within the Flex applications I am building. However, I am a bit surprised that I have not found much push back online from the Flex community on introducing the high level of abstraction that comes with the Cairngorm framework.

I absolutely believe in having some consistent structure for separating view logic from model logic (and so-on), but it just seems that Cairngorm could be a whole lot flatter. My first pass at writing code with the framework takes me back a few years to when my peers and I where implementing the Session Facade pattern in web applications that where deployed in a single container, and were never going to have more than 10-20 total users. In hindsight, this was obviously totally unnecessary and an inappropriate use of the design pattern. It seems like Cairngorm could be taking us down a similar road here.

Regardless, I have decided to use Cairngorm in my first Flex application (it seems to be the Flex MVC framework with Adobe’s endorsement), as I hate spending a lot of time writing my own plumbing, especially when I am still getting up-to-speed on a language (i.e. Flex). However, my usage of the framework comes with a number of concerns upfront. I thought I would outline my initial reactions and concerns, and see if they still hold up after using Flex and Cairngorm for a while.

The first resource I used to gain an understanding of Cairngorm was their overview diagram of the ‘Cairngorm 2.0 Microarchitecture,’ found at: http://www.cairngormdocs.org/cairngormDiagram/cairngorm2_rpc.swf.

My immediate reaction after viewing the diagram was, ‘Wow! The whole Core J2EE Patterns book duplicated. Really?’ It would appear that the Cairngorm developers owe Deepak Alur, Dan Malks, and John Crupi a big thanks for their contribution of the Core J2EE Patterns. As someone who has used, and at times abused these patterns, I am not completely sold that this is an appropriate place to utilize them (at least not to this extent).

My first question is, why do I need this level of abstraction when there is a natural SoC in the client-server like programming model of Flex? Think back a few years to early web development in Java. With Java I can issue SQL statements directly from a JSP (and many developers did/still do). Most everyone agrees this is bad, thus the Java community responded with countless MVC frameworks to enforce more discipline and give direction to misguided folks who would do things like issuing a SQL statement directly from a JSP.

Flex does NOT offer any API’s for database access, as it is just a client-side presentation framework. If you want to access an SQL database you have to go through a business services layer (e.g. SOAP, Data Services, etc…) that encapsulates the logic for accessing the appropriate data. Thus, you have a natural SoC in Flex that does not exist in pure Java deployments.

My next question, why introduce a Front Controller into an event programming model like Flex? The Front Controller seems to be a perfect fit for Struts, as it was building on Servlet/JSP development where there was little notion of state and no event model. In addition, the entire flow can be controlled in a Flex based application, unlike traditional browser based deployments where a user could potentially jump into an application at any point by simply providing the URL.

Also, in the example applications the Front Controllers do not seem to be doing anything except dispatching custom events. No security checks, no decision points on redirecting the view (as Flex provides View States for this), none of the typical benefits outlined in the pattern.

Moving through the diagram, the Business Delegate makes good sense to me as a way to encapsulate interactions with the server. Although, I am surprised that the logic that ends up in the Commands (in the example applications) isn’t just part of the Business Delegate. I tend to view the delegate as a client that makes the call, does any necessary translation, and returns meaningful data types to the client side application. The delegates in the samples pretty much just provide a handle to the remote services, and the Commands make the call using the Delegate and return the meaningful types to the Flex application.

The Service Locator seems to make good sense, as a way to organize and access the different services needed for a given Flex application. Although, it does not appear to be a true Service Locator, but just a way to encapsulate the client side configuration for accessing the remote services.

I think that the custom events and value objects also make good sense, as they would be apart of any implementation that moves the model and flow logic outside of the mxml components.

Anyhow, after all of my analysis… I do still plan to use Cairngorm for an implementation I am coding in Flex, and then review my initial thoughts outlined in this post. Although I think it is overkill, I do believe it provides me the SoC I am looking for, and is good enough for right now. So, on to writing code!!!

Resources

http://www.cairngormdocs.org/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Digg
  • del.icio.us
  • Reddit
  • Facebook
  • LinkedIn

Comments:
Stefan Schmalhaus on May 21st, 2007 at 2:08 am #

You may want to read the very interesting discussion of MVC/Cairngorm on Sho Kuwamoto’s blog:

http://kuwamoto.org/2005/08/16/mvc-considered-harmful/

Mark on May 21st, 2007 at 4:47 am #

Interesting Analysis…

Though you can access Databases more or less using Flex Data Services (now known as LiveCycle DataServices).

I am no expert, but I found the Commands to be great at encapsulating ALL of the code that affects the model away from the View or Delegate.

I think the delegates are good to be kept seperate from the Commands to allow for unit testing, also, it means that 99% of the code is handled in the command… which is immensely useful in debugging a complex application.

Jon on May 21st, 2007 at 6:14 am #

Jon,

Great write-up on Cairngorm. I’ve also wondered about a few of your points, but you definitely raised some new questions for me. Do you think that Cairngorm could be improved to better leverage the client-server model for a more “natural” SoC?

I would really like to see a response from a Cairngorm “expert” or someone on the development team. It’s always beneficial to see multiple perspectives.

James Ward on May 21st, 2007 at 6:39 am #

Hey Jon,

I’m glad you are looking at Cairngorm. I’m working on an article about MVC in Flex. Sometimes Cairngorm is overkill. For simple applications with a single developer you can just use the Service Locator, Model Locator, Delegate, and Value Object patterns. But I think in most non-trivial Flex applications those are a minimum. If you are just using those patterns Cairngorm probably isn’t necessary but it might be helpful if you plan to someday be using Cairngorm in your app. Anyways, this is a great write-up! Thanks!

-James

James Ward on May 21st, 2007 at 6:52 am #

I forgot to say… This blog helps me to know what to focus on in the article I’m writing. need… coffee…… too… early…

jonr on May 21st, 2007 at 9:06 am #

Thanks for the comment Mark.
To clarify a bit, Flex does not offer any client side ways of accessing a database that I am aware of. If they ever do, it seems to me that it would get a bit scary from a security perspective. As you said though, they absolutely have Flex Data Services (server side) for providing access to backend services like the db, thus the natural SoC I am referring to in the deployment model.

justin on May 21st, 2007 at 10:24 am #

I couldn’t agree more. I ended up rolling one consisting of view, controller, dataManager (service and data tranformation in one), session (modelLocator + transient session data)

JesterXL on May 21st, 2007 at 11:23 am #

I come from a Flash / designer background, so here are my responses.

1. why do I need this level of abstraction when there is a natural SoC in the client-server like programming model of Flex?

Delegates are invaluable when your backend is:
A) not done
B) not using Remoting

For A, if the API changes, the rest of my app gets the same data, I only have to change 1 class per call.

For B, if I’m not using Remoting, I have to parse data and make ValueObjects out of it. The Delegate handles all this mess with Factories. Again, the rest of my app gets the same data, no matter what the data is (XML, URL encoded vars, etc.).

Commands are then allowed to focus on just data handling, typically the core of the app. Since the Views are bound to that data, this heavy lifting code is not polluted with data parsing.

I can see later you dig Business Delegates too. You must understand, though, Commands can get VERY hairy since you are doing the most important updateing and changing of data. Keeping these classes simple and short makes your most important code easy to read, upodate, and maintain.

2. why introduce a Front Controller into an event programming model like Flex?

Agreed. To me, it’s just a formality. There ARE people who build wrappers around this that address the concerns you talk about, hence Cairngorm v2.2 having the ability to use weak listeners as well as removing commands programmatically. I still feel it’s a leftover to make Java devs comfortable.

ablesa on May 21st, 2007 at 3:02 pm #

Hey Jon, interesting point of view…
Maybe you’d like to take a look to guasax framework(http://www.guasax.com/blog), we are working in a altenative to cairngorm for resolve the same problem.
I hope you found this information useful.

Bjorn on May 21st, 2007 at 5:01 pm #

I’m a fan of using Delegates with Remoting.
I’m not sure how others are using them but my delegates basically mirror the DAO classes.
That way i have a client-side interface of the exposed DAOs.

Some areas of Cairngorm initally seemed overkill to me as well until I was assimilated and accepted that it is an industry standard framework and hopefully new developers coming into my project will find it easier to grasp because of their familiarity with Cairngorm.

I am always interested to read and try alternatives :)

Damien Jorgensen - Wales on July 7th, 2007 at 1:09 am #

I am just startig in Flex, interesting topics raised here

Tanuvan on August 3rd, 2007 at 11:21 pm #

Overkill!!! Perhaps to pacify the Java fans. The Cairngorm just seems way to much for being practical. Value Objects are ok. It seems to me that Flex2 is naturally event driven, but some of these frameworks seem to complicate even the simplest of things.

[...] have been rebelling against dragging additional frameworks into Flex just for MVC for a while now (Flex and MVC /  Cairngorm).  I finally got a chance to spend some time coding with Mate on a flight back from [...]

[...] consider using Flight, as I still don’t understand what value Cairngorm provides (old post 1 & 2).  That doesn’t mean I don’t believe that Flight may have value, but in [...]

[...] I first saw Cairngorm (post), it was easy to understand what they were trying to do, as the framework is so heavily influenced [...]

Post a comment
Name: 
Email: 
URL: 
Comments: