【问题标题】:GWT/AppEngine: Designing RequestFactory workflow for better exception handlingGWT/AppEngine:设计 RequestFactory 工作流以更好地处理异常
【发布时间】:2014-01-15 22:57:56
【问题描述】:

让我们看看一个通常的 RequestFactory 服务器端代码,试图找到一个学生。

Student s = null;
try{
    ....
    s = pm.findStudent(....)
    ....
}catch(Exception e){
    ....
}finally{
    pm.close();
}
return s;

如果出现异常,'s' 有可能保持为空。但是客户端代码将很难确定是否有异常或没有找到学生。因此,如果我删除异常块:

Student s = null;
try{
    ....
    s = pm.findStudent(....)
    ....
}finally{
    pm.close();
}
return s;

我将能够使用 RequestFactory 的 onFaliure 在客户端处理异常。这将帮助我轻松地对这两种情况采取必要的措施,即如果没有找到学生 v/s 则存在异常。

我想了解这是否是正确的方法、潜在的陷阱,还是有更好的方法?

【问题讨论】:

    标签: google-app-engine gwt requestfactory


    【解决方案1】:

    您必须考虑请求(方法名称和参数)和响应(返回类型)。

    如果您想返回一个对象和/或错误,请使用带有两个属性的ValueProxy,例如

    @ProxyFor(FindStudentResponse.class)
    interface FindStudentResponseProxy extends ValueProxy {
      StudentProxy getStudent();
      FindError getError();
    }
    

    FindError 将是一个枚举。

    【讨论】:

    • 是的,这是我知道的另一种选择。我的查询更多的是在服务器端跳过异常处理并在客户端处理它的方法,以帮助我们区分成功和不成功的调用,是否是一个好方法。潜在的陷阱?
    • 不是。一个问题是如何将故障类型传达给客户端;您可能会在ServerFailure 消息中定义自己的协议。 RF 的 onFailure 与 GWT-RPC 的 onFailure 不同。
    猜你喜欢
    • 1970-01-01
    • 2012-09-13
    • 2011-07-02
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多