Tuesday, January 09, 2007

So after doing a small Flex project last fall. I decided to tackle Cairngorm. After reading a ton of the documentation I am convinced that Cairngorm is a great way to deal with binding data and maintaining state throughout an application. So, I get the theoretical side of things and I think I even understand all that needs to be done to build a Cairngorm flow (or "Round Trip" as I call it). However, actually writing the code is another thing. Here is how I understand it and maybe this can help you as well (or at least you can tell me where I am going wrong).

For every action that needs to get and return data we call a series of classes in order to dispatch the event, call the service and return the data to the ModelLocator (which of course is bound to the views and makes updating the view easy.) You can flow this flow in a wonderful diagram here http://www.flexheads.com/cairngorm/cairngorm2_rpc.swf. Once you understand this (or at least understand the concept), then you actually have to write each file. Basically there are 4-6 files that need to be written (or updated) for EVERY new event that you want to call:

1) ModelLocator.as (in model dir)
- there is only one of these (in best practice) and you just keep adding code to this each time you create a new event.

2) YourNameEvent.as (in event dir OR control dir - I have seen both)
- Substitute "YourName" with the name of your event and then create a new one of these for each event.

3) Controller.as (in control dir)
- There is only one of these which you add a couple line of code to each time you create anew event.

4) YourNameCommand.as (in command dir)
- Same as the Event above. There should be one of these for every command you are calling.

5) YourNameDelegate.as (in business dir)
- There should be one Delegate for event command UNLESS you don't need to go back to the server for data. In that case you can skip the Delegate step.

6) Services.mxml.as (in business dir)
- There should only be one Services file which defines the services used in the application.

Obviously there are probably exceptions to all these guidelines, but I think when starting out it is best to simplify as much as possible.

So in practice what this means is that if I decide I want to have an event called, "GetUserInfo" then I will need the following new files:

GetUserInfoEvent.as
GetUserInfoCommand.as
GetUserInfoDelegate.as (*assuming you are going to the server)

AND I will need to add code the following files:

ModelLocator.as
Controller.as

(The Services.mxml file you should only have to update when you are defining a new service. Therefore if all your info is coming from the a database, then you set this up once and not touch it for a while.)

So that doesn't seem quite as bad. In fact, it is possible to quickly just stub out the code every time you create a new event to just set up your framework and in fact another fellow flex coder has done just that by creating a program which will create all the stub code needed for each class by just entering the name of the class. http://www.ericfeminella.com/blog/2007/01/04/introducing-cairngen/. Unfortunately, I couldn't get this working on the Mac yet, and it also only supports Cairngorm 2.0 at this point, but it is easy to see how something like this can speed up your code greatly.

Thus my next task is to create my 2.1 templates and then build out all my stub code using this. Once that task is done, I mearly have to modify the command file to put in the event logic and I should be good to go....if only it will happen that easily.