CollectionEventKind.RESET, WTF Does That Mean? Loading Modules/Runtime CSS into an AIR App
Feb 07

Lately I’ve been experimenting and researching with a lot of various UI architectures that either derive from model-view-controller, or depart from it completely.

After using Cairngorm for a dozen or so projects, you begin to ask yourself, “Is there something out there better than this?” I literally felt like Cairngorm turned me into a robot – create event, create event type, create command, add to front controller … create event, create event type, create command, add to front controller … wash, rinse, repeat. This mundane process finally put me on a quest for a better solution.

Some argue that Cairngorm is best suited for enterprise level applications where you have multiple people all working on the same project, and sharing the same files. From my experiences, I would disagree. You wouldn’t believe how many duplicate events and commands we had splashed around our applications. I think this is attributed to Cairngorm’s lack of defining a uniformed structure of an application. Oh yea, and lets not even start on the subject of where you have the multiple instances of a view being tied to a single model.

In my search for a better MVC solution, I had a few guidelines that the pattern should meet:

  1. Separating pieces of an application into their own modules or “sections.” By doing this, it makes it easier to replace UI interaction, or add feature XYZ when your boss comes knocking on your door.
  2. Reduce dependencies between each “section” of an application. I know, I’m asking a lot here; however, just because a component in a panel on the left side of your app communicates with another panel on the right side of your app, doesn’t mean they need to have direct dependencies between them.
  3. Reduce dependencies between the model and the view. In traditional MVC and Cairngorm, the view is allowed to be coupled to the model, and the model indirectly tied to the view via the Observer pattern (i.e. data binding). By untying the view from the model, you now make your views more robust by being able to use it with an albeit similar, but different model.
  4. Being able to work with the Flex module loading framework. Cairngorm has some issues related to this, and there are some workarounds for it, but generally speaking, Cairngorm wasn’t designed with module loading in mind.
  5. Completely free of singletons. Singletons have their time and place, but a model should never be a singleton! This is like putting your data in concrete. Just because you think now that you’re never going to need multiple instances of an object, doesn’t mean that you won’t have to in the future. On top of that, singletons are nearly impossible to subclass. At the very least, have your singleton classes conform to an interface. This way you can change its implementation in the future. Ok, I’m getting off topic ..

MVC meet H-MVC – Hierarchical MVC
From my research, H-MVC is a pattern used mostly in the Java world, and is unknowingly based off of the PAC (Presentation-Abstraction-Control) pattern. H-MVC answers the following questions when architecting a UI:

  • What’s a consistent way of structuring my UI code?
  • What’s a good way to cleanly separate my views from my domain model, and make them reusable?
  • What’s a good pattern to easily allow for application flow, and inter-component communication?

At its core H-MVC is an event based framework that makes a clean break between your models and your views. On top of this separation, it effectively allows your views and models to communicate with other parts of your application by simply firing events. This communication works by controllers propagating these events throughout the whole application. Think event bubbling in Flash, but instead of this happening through the views, it happens through the controllers.

H-MVC Diagram
Image taken from the JavaWorld HMVC article.

Let’s take the example where Component A is its own view that contains a single button. We have another view called Component B that contains a ViewStack. On top of that, Component B is nested somewhere in our application that is completely outside the visibility of A. When the button is clicked in A, we want B to switch the selectedIndex of its ViewStack to 2. H-MVC successfully decouples the communication by making the assumption that a parent will always know about its children, but the children never knows about its parent.

Since this post is getting pretty long, I’ll hold off showing any code examples till my next post. In the mean time, if you want to do a little reading on H-MVC, check out the original article written by Jason Cai, Ranjit Kapila and Gaurav Pal. There’s also another article that’s dramatically shorter and gives a good overview.

5 Responses to “MVC meet H-MVC – Hierarchical MVC”

  1. Daniel Wabyick Says:

    Hi! I think this is the start of a really good article. In the Actionscript world, people just assume that Cairngorm is the answer. After evaluating Cairngorm, I determined that the whole event->command mapping was unnecessary, and just added confusion to the code.

    Also, I think HMVC is a good one. I usually have an ApplicationUIModel and ApplicationModel, for top-level application state, and then a similar pair for each major section of the application. This tends to work well. For controllers, I tend to put that logic into Command classes, which compartmentalizes functionality better than a multipurpose ‘controller’.

    Additionally, I think your point about avoiding singletons for models is a good one, although I still use it as the convenience factor for small projects is hard to resist. I guess ModelLocator is a good aspect of Cairngorm that could be used for this purpose.

  2. Darren Says:

    This approach looks really interesting and I look forward to your next post. However, I don’t think all of your criticisms of Cairngorm in enterprise applications are well founded, particularly with regard to Cairngorm not being prescriptive enough about telling you how to structure your application and avoid duplication. From what I have read from the Cairngorm developers, they have deliberately left these decisions up to you. In the end, it does require some work on the part of your team to make structural decisions early in the development process, or perhaps your company might develop their own standards regarding this. If you make the right decisions, you shouldn’t have the problems with duplication you are talking about. There’s many areas of Cairngorm that often require extending or tailoring for specific projects. I guess it’s up to the individual as to whether this more lightweight, flexible approach is a strength or weakness of Cairngorm.

  3. david_deraedt Says:

    Hello. I can’t wait to read your next post ;) There’s a couple of things I noticed in this article :
    - Concerning your #3. I personnaly think that Flex databinding is a great way to decouple View and Model. But if you think you should go another way, what do you think of PureMVC’s Mediators?
    - Concerning your #5 , I don’t think the reason why you should get rid of Singletons is because your data is “put in concrete”. That’d be the right metaphore if you used static classes to store your data. But Singletons are precisely good at creating an object that’s unique for the moment but could have multiple instances later. IMHO, The main reason why we should avoid using Singletons is because it’s a pain in the ass to debug them (think unit testing hell).

  4. dschultz Says:

    Hey David,

    When I say you put your “data in concrete,” I’m saying that your whole application can have only one instance of that particular model instantiated at any given time. What I was trying to convey in #5 was that you should NEVER assume that you’ll have only one instantiated model (or any given object, unless it is absolutely essential to your app) at any given time throughout the lifetime of your application. This assumption, in my eyes, is pure ignorance. Again, this refers to the problem in Cairngorm where you have multiple instances of the same view referencing a single model.

    Concerning data binding and decoupling the view from the model – this is a tough one ;). By using Flex data binding, you’re inherently assuming that a view should be conforming to a model. For example, by having a view data bind to a property XYZ in the model, you’re assuming that the model actually has a property called XYZ. I think Apple (of all people) has solved this conundrum quite effectively with their Cocoa framework, by creating an MVC architecture for data binding in itself. This is: by creating a controller that is a mediator between the view and a single object in the model. Hopefully this makes some sense. I’m starting to get pretty abstract :)

    Dan

  5. Pages tagged "m*a*s*h" Says:

    [...] bookmarks tagged m*a*s*h MVC meet H-MVC – Hierarchical MVC saved by 1 others     nitch23 bookmarked on 02/08/08 | [...]

Leave a Reply