Learning to love Cairngorm!
Cairngorm is an Adobe-endorsed, industry-standard Model-View-Controller [MVC] framework for the Flex platform. Based on well-known j2EE design patterns, the MVC framework defines an design approach for scalable, enterprise-level software solutions. Cairngorm is not new!
MVC frameworks are used everywhere.
- Java developers use Struts,
- ColdFusion developers use mach-ii,
- Flash developers use ARP, and
- Flex developers use Cairngorm.
Not only is MVC framework based on a set of architectural heuristic (rule-of-thumb)... it is serves as a best-practice for creating great software. Using MVC helps to avoid those notorious conditions of "spaghetti-code" and component couplings that kill any component reuse.
Remember it is not only important for your MVC implementations to be quick-n-easy to use, it is essential for the code to be extremely stable when changes occur. Other developers - new to the project - should also be able to easily maintain and enhance the existing code base. The Cairngorm MVC provides a framework that helps accomplish all these objectives!
Question: Is Cairngorm only for large Flex projects?
Absolutely not! We have all worked on projects where only one view or screen was initially required; only to discover scope changes that demanded more screens, more business logic, more complexities. Small projects that use the Cairngorm MVC designs can easily scale and allow views to be radically changed or reused in other composite views... without changes to business logic or data access.
Cairngorm simple defines connections or relationships between the view(s), the model data, and the business logic (controller). Developers only need to remember the following 3 design principles:
Views:
- Never know where or how the data is loaded or persisted
- Show data in rich formats
- Allow users to interact with the data & controls
- Use events to announce user interactions or changes to data
Controllers:
- Are used for loading or persisting remote data or
- Are used to coordinate activity between completely separate components within the Flex application
- Never know how a view(s) show the data. To controllers, views are "black boxes" where the internals are unknowns
Models:
- Are simply global areas to store & access data already present in-memory. This data can be structures or objects or both...
- Are containers for business data & settings to allow views to share data & to coordinate state
Question: Why do so many developers resist using Cairngorm?
Many developers have implemented their own MVC version. Unfortunately those version present maintenance issues and have not been adopted as an industry standard; Cairngorm is an Flex industry standard and is well known to a wide range of developers and development teams.
Another common response is that the Cairngorm implementation creates so many classes and files that the application becomes "heavy" and impossible to manage or track.
Many developers - even architects - do not welcome Cairngorm implementations because they have not optimized its implementations and feel that real-world uses resulted in distorted MVC solutions.
Question: Okay, so what is wrong with Cairngorm?
The issue is not that Cairngorm is wrong... more that the Cairngorm implementation needs to be optimized. "WTF does 'optimized implementation' mean?" To answer this question, allow me to make some bold statements.
Deprecated Cairngorm classes
- ViewHelpers should NEVER be used
- ViewLocator should NEVER be used
Extensions to Cairngorm ("Optimized Implementations")
- Commands should respond to-/handle multiple events
- View instances need ways to be directly notified of event responses.
- CairngormEvents should bubble... so dispatchEvent() can be used
Best Practices
- Use of the Delegate is optional but recommended
- State should be maintained & localized within views
- View-specific data should be maintained/localized in the view
- Only application-wide data should be maintained in the Model
- Model.getInstance() should only be referenced by "top" parent views
- Only "global" events should be dispatched to controllers.
Above, I have enumerated the areas in which Cairngorm may be used and improved. In upcoming blog entries, we will review details and see how an optimized Cairngorm [more lightweight & optimized] can be the solution for almost every project... even the small ones!
With an understanding of the above recommendations, developers will quickly realize the benefits of the Cairngorm MVC and grow to love its versatility and benefits in most of their Flex projects. With my next blog entries, I will elaborate with details on my bold statements above.
Meanwhile below is the actually source code changes to the Cairngorm release(s). I will be submitting these changes to the Cairngorm committee in hopes that they will become part of the standard release.
Here are some resources:
1) Cairngorm Docs
2) Cairngorm Explorer
3) Cairngorm 2.1 Download
4) Optimized Cairngorm 2.1 Download