Cairngorm故障处理程序

时间:2011-03-29 15:22:47

标签: flex cairngorm

根据Cairngorm架构,我们总是在每个服务的每个命令类中都有一个错误处理程序。

我们如何创建单个类来处理所有服务的故障处理程序事件。

2 个答案:

答案 0 :(得分:1)

创建一个扩展所有其他类的基类,将错误处理程序放在那里。如: FaultHandlerCairngormCommand扩展SequenceCommand实现IResponder

[BaseCommand.as]

public class BaseCommand extends SequenceCommand implements IResponder
{
    public function execute( event:CairngormEvent ):void 
    {
        super.execute(event);
    }

    public function fault( info:Object ):void 
    {
        throw new Error("Generic request failure"); //or handle as you please
    }
    public function result(data:Object):void
    {
        throw new Error("The result method implementation defined in IResponder for all extensions of BaseCommand must be overrriden in any sub-class");
    }
}

[MyCommand.as]

// -- no need to implement onFault in sub-class
public class MyCommand extends BaseCommand
{
    public function execute( event:Event ):void 
    {
        remoteObjectDelegate.doYourServerOperation(this);
    }
    override public function result(data:Object):void
    {
        trace("happily handling the data"); //without this override an error will be thrown so the developer will know to correct
    }
}

答案 1 :(得分:1)

通过“总是有一个故障处理程序”,你是指合同,如实现一个接口?

您可以编写所有其他命令类扩展的基本命令类。基类可以实现on fault handler,所有其他子类都可以选择覆盖它。

public class BaseCommand implements ICommand
{
    public function execute( event:Event ):void 
    {

    }

    public function onFault( event:Event ):void 
    {

    }
}

// -- no need to implement onFault in sub-class
public class MyCommand extends BaseCommand
{
    public function execute( event:Event ):void 
    {

    }
}