Thursday, November 08, 2007

Cairngorm should take a lesson from Rails

On Monday I was adding some new functionality to viibee.com and it occurred to me how tedious it is to do that kind of stuff within Cairngorm. Just like DHH calls it: "XML sit-ups" (though it's not XML, but the sit-ups are the same).
The problem is:

  1. There is a lot of repetitive wiring to be done
  2. In our situation, where all client-server interactions follow the same pattern, one interaction could safely be deduced from a minimum of information.
For example: let's say I want to create the new "list users" functionality. I need to create a "list users" event, wire this event into the controller so that it fires up the "list users" command, create a method "list users" in the delegate, define "list users" in services.xml and, finally, code the "list users" command. And the same for "add user", "delete user" etc. You get the picture.

I think that Cairngorm should have a drink from RoR's "convention over configuration" cool-aid. How could this work?

Suppose you fire the event

new CairngormEvent("list_users", [rest params])

You do not have to define this event type. By convention, the controller will execute the ListUsersCommand. This ListUsersCommand is the piece you actually have to implement. Within ListUserCommand's execute method you call the delegate as usual. But, you should not have to define, what the delegate is supposed to do. Something like

delegate.listUsers(params)

should suffice. The delegate knows that the method "listUsers" translates into "/list/users" and dispatches the request onto its default server. The parameters are mapped onto CGI-style parameters, potentially converting camel-case into underscore_case. The response will automatically be sent to the command.

Of course, one should be able to overwrite, i.e. explicitly define the default behavior. But that can be done already.

Maybe, I am biased because in our project all client-server interaction is rather uniform, but I can well imagine that others go through the same kind of repetitive exercise. IMO this would be a worthwhile update to Cairngorm.

No comments: