Structuring the application
When building Flex applications with Cairngorm, I begin by setting up the structure of the application. This allows me to focus on utilizing this structure whenever I'm adding new features. To start this project, I created the Model Locator. This is just a Singleton which implements the com.adobe.cairngorm.model.IModelLocator marker interface. Its function is to provide a central storage and access point for the application data and domain.
Since the data for the application comes from an external XML file, both the Service Locator and the Business Delegate patterns within the Cairngorm framework are utilized.
The Service Locator is a Singleton, usually extended with MXML, containing all of the services the application uses. The application itself only contains an HTTP service pointing to the URL of the XML file.
The Business Delegate abstracts the specifics of calling various services away from the main business logic. Since the image viewer application has one service, it only requires one Business Delegate. The Business Delegate has a function called getData. When this method is called, the XML file is loaded and an ArrayCollection of value objects is created from its contents.
The first Command class that is required is executed when the application starts. It calls on the service defined within the Service Locator via the Business Delegate. Once it receives the results, it simply updates the ArrayCollection in the ModelLocator with the one received from the Business Delegate.
At this point, the only thing the application can do is load in some XML. However, by setting things up this far, a large amount of the application's structure is already in place. Here's a summary:
- The Model Locator contains an ArrayCollection of image value objects.
- The Service Locator and Business Delegate implementations define the service (the XML file) and encapsulate its loading.
- The Command class calls a method within the Business Delegate and updates the model with the result.
- The Front Controller registers the one command to an event. The event fires when the application loads, causing the data to be immediately loaded.