Wednesday, August 23, 2006

CairngormEvent Anti-Pattern!

We all know that the big downside to the Cairngorm framework when building Flex apps is the explosion of classes that result. My big issue is that to add, edit, and delete a widget, you have to create 3 seperate classes named "AddWidgetEvent", "UpdateWidgetEvent", and "DeleteWidgetEvent"....3 classes that are identical EXCEPT for their class name and command name that is passed to the contructor....

package com.widget.control{
import org.nevis.cairngorm.control.CairngormEvent;
import com.widget.control.WidgetController;
import com.widget.vo.WidgetVO;

public class AddWidgetEvent extends CairngormEvent
{
/** Constructor */
public function AddWidgetEvent(widgetVO : WidgetVO){
super(WidgetController.EVENT_ADD_WIDGET);
this.widgetVO = widgetVO ;
}
public var widgetVO : WidgetVO ;
}
}

I don't see the point of creating separate CairngormEvents for each action. Instead, pass in the commandName into the single, resuable event. So the call would look like this:

var addEvent : WidgetEvent = new WidgetEvent(WidgetController.EVENT_ADD_WIDGET, widgetVO);
dispatchEvent(addEvent);

And the CairngormEvent for ALL the widget events can now be a single class like so:
package com.widget.control{
import org.nevis.cairngorm.control.CairngormEvent;
import com.widget.control.WidgetController;
import com.widget.vo.WidgetVO;

public class WidgetEvent extends CairngormEvent
{
/** Constructor */
public function WidgetEvent(commandName : String, widgetVO : WidgetVO){
super(commandName);
this.widgetVO = widgetVO ;
}
public var widgetVO : WidgetVO ;
}
}

No comments: