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.

Wednesday, December 27, 2006

to give a little background...

I having been developing Flash RIA apps for the past couple years and although I consider myself a hack compared with more schooled programmers, I also have become somewhat proficient at designing useful RIA's for clients. The problem became that I designed most of these apps with limited knowledge of design patterns or OOP and thus collaborating with other developers and designers became nearly impossible as I was the only one who knew what was where and who was what.

So... i decided to work on migrating my most popular RIA from a jumbled mess (that somehow works well!) into an organized MVC RIA (or as close as I can get without going crazy). Flex immediately became very exciting to me as it provided many built in features, components & structures, that I had pulled my hair out trying to create for myself in Flash. In addition, the whole philosophy behind developing Flex apps spoke to that part of me that so desperately wants to be able to hire people to build these apps with me! So I jumped in.

I built my first Flex app (using AMFPHP) in Nov and although there were some major hiccups, I got the general idea quickly. So now it's time to tackle the next step... build a solid MVC app. Unfortunately, I have found that there are some "supposedly simple" things that I can't figure out. I think I understand the theoretical concepts, but then I can't seem to get past something as basic as "Install Cairngorm"! I don't know if any of your have run into these types of problems, but if so, this blog is for you. My goal is to document some of the simple tasks in Flex that seem to elude and bog down those of us who are just starting out.

I am not going to attempt to provide much organization or structure to this blog, but instead just document anything that when I finally figure it out, makes me go "oh of course...I get it!"

If you have any problems you are trying to solve, feel free to send them to me and I will see if I can help. Also, remember I am a hack so PLEASE add your comments if you know of a way to do this better. It is my attempt to get the discussion started, not necessarily solve the problem eloquently. As I get better at this, I am sure I can tackle more important problems, however, for now I will just try to figure out how to turn on my computer.

All the best,

Kevin