Cairngorm Microarchitecture
Pronunciation
Cairngorm (kârn gôrm) n. yellowish-brown variety of
quartz, especially found in Scottish Cairngorm mountain.
What is it?
Collection of classes/interfaces against which we can compile
Elegant collaboration and partitioning of design responsibilities
"microarchitecture" - Provide the skeleton or internal structure of moving parts, around which the flesh and muscle particular to a business domain can be layered
A standard way of building a Flex app.
Why use it?
Multiple developers working on the project, and run the risk of them all solving the same problem in multiple different ways
Unified approach to problemsInject complex functionality in a well
understood manner Promote, enable and encourage reuse
(business objects, business services, etc.)
The Pieces of Cairngorm
Model Locator: Stores all of your application’s Value Objects (data) and shared variables, in one place. Similar to an HTTP Session object, except that its stored client side in the Flex interface instead of server side within a middle tier application server.
View: One or more Flex components (button, panel, combo box, Tile, etc) bundled together as a named unit, bound to data in the Model Locator, and generating custom Cairngorm Events based on user interaction (clicks, rollovers, dragndrop.)
Front Controller: Receives Cairngorm Events and maps them to Cairngorm Commands.
Command: Handles business logic, calls Cairngorm Delegates and/or other Commands, and updates the Value Objects and variables stored in the Model Locator
Delegate: Created by a Command, they instantiate remote procedure calls (HTTP, Web Services, etc) and hand the results back to that Command.
Service: Defines the remote procedure calls (HTTP, Web Services, etc) to connect to remote data stores.
Cairngorm 2 Microarchitecture
Model
All client “state” dataAnything retrieved from serverImplements ModelLocator Cairngorm interfaceThe model locator in an application is a singleton
that the application uses to store the client side model.
Summary: Model is a Singleton containing data.
View
User Interface.mxml files w/controls (text fields, combobox,
datagrid, etc.)All data is pulled from model via a binding
Controller
Allows communication between tiers of application via Events
extend class com.adobe.cairngorm.control.CairngormEvent
Commands implement interfaces
com.adobe.cairngorm.commands.ICommandcom.adobe.cairngorm.business.IResponder (optional)
Tie Events and Commands together in Controller class
Conduit between user events and model changes
Events
Events classes usually just a Collection of properties Constructor with params to populate those properties
Used to pass data between layers of an application
Event - example
package com.echoeleven.controller.event{import com.adobe.cairngorm.control.CairngormEvent;import com.echoeleven.controller.ApplicationController;
public class LoginEvent extends CairngormEvent{
public var username:String;public var password:String;
public function LoginEvent(username:String, password:String) {
super(ApplicationController.EVENT_LOGIN);
this.username = username;this.password = password;
}}
}
Command
“Service to Worker” command patternMust implement the Cairngorm Command interfaceIf receiving data from server (e.g. RemoteObject),
should also implement Responder InterfaceCommand class must implement execute() methodexecute() usually takes an event argument as a
parameter
Command - example
package com.echoeleven.controller.command{
import com.echoeleven.model.ApplicationModel;import com.echoeleven.controller.event.LoginDataChangeEvent;
public class LoginDataChangeCommand implements Command{
public function execute(eventParam:CairngormEvent):void{
var event:LoginDataChangeEvent = eventParam as LoginDataChangeEvent;
var appData:ApplicationModel = ApplicationModel.getInstance();
appData.username = event.username;appData.password = event.password;
}}
}
Controller
Tie Event and Command classes together in Controller class constructoraddCommand( ApplicationController.EVENT_LOGIN, LoginCommand );
Controller listens for eventWhen event is dispatched, command’s
execute() method called (by controller)Conduit between user events and model
changes
Controller – Big Picture
1. View event triggered (e.g. click event)2. Caught by private method in view (mxml file)3. View method creates instance of event
(extending CairngormEvent)4. View method dispatches that event via
CairngormEventDispatcher5. Controller catches CairngormEvent and
executes associated Cairngorm Command
private function btnClick( event:Event ):void {var evt:LoginEvent = new LoginEvent(userName.text, password.text);
CairngormEventDispatcher.getInstance().dispatchEvent( evt );}
Example Flow
Create LoginDataChangeEvent classCreate LoginDataChangeCommand classConnect Event to Command in controllerCatch the TextField.change event, and call local
function dataChange()Create instance of LoginDataChangeEvent and
dispatch itLoginDataChangeCommand.execute()
automatically calledexecute() method modifies ModelChange to model reflected in View through binding
Service Locator
SingletonAbstracts data communication layerDefines which protocol (http, SOAP, AMF,
etc.) and which endpoint params (which service, which channel, etc.)
Business Delegate
Who should handle the results of a server operation?
Avoids attaching result/error routines to data services (e.g. RemoteObject)
Allows Command to call remote object method, and handle the result and fault methods
Multiple instances may exist
Business Delegate
The Business Delegate typically fulfills its role in collaboration with the Service Locator; it uses the Service Locator to locate and look up remote services, such as web services, remote Java objects, or HTTP services. Once located, the Business Delegate invokes these services on behalf of the class that has delegated responsibility to it for business logic invocation.
References
Cairngorm DocsBorrowed contents from Scott Talsma’s slide