0

假设我正在尝试使用 GWT 的 RequestFactory 在客户端和服务器之间双向传递不可变类型。假设底层类型是 TimeOfDay,它被设计为不可变的:

public class TimeOfDay {
  private final int hours;
  private final int minutes;

  public TimeOfDay(final int hours, final int minutes) {...}
  public int getHours() {...}
  public int getMinutes() {...}
}

我可以用 ValueProxy 代理这个类:

@ProxyFor(TimeOfDay.class)
public interface TimeOfDayProxy extends ValueProxy {
  public int getHours();
  public int getMinutes();
}

现在,我可以很容易地在服务器端创建 TimeOfDay 实例并将它们返回给客户端,通过服务器端的这个:

public class TimeOfDayService {
  public static TimeOfDay fetchCurrentTofD() {
    return new TimeOfDay(16, 20);
  }
}

...这在客户端:

@Service(TimeOfDayService.class)
public interface TimeOfDayRequestContext extends RequestContext {
  Request<TimeOfDayProxy> fetchCurrentTofD();
}

...
final Receiver<TimeOfDayProxy> receiver = new Receiver<TimeOfDayProxy>() {...};
final TimeOfDayRequestContext context = myFactory.timeOfDayRequestContext();
final Request<TimeOfDayProxy> fetcher = context.fetchCurrentTofD();
fetcher.fire(receiver);
...

这很好用。但是,如果我在相反的方向尝试这个,我会遇到障碍。即,在服务器端:

public class TimeOfDayService {
  public static void setCurrentTofD(final TimeOfDay tofd) {...}
}

...在客户端:

@Service(TimeOfDayService.class)
public interface TimeOfDayRequestContext extends RequestContext {
  Request<Void> setCurrentTofD(TimeOfDayProxy tofd);
}

...
final Receiver<Void> receiver = new Receiver<Void>() {...};
final TimeOfDayRequestContext context = myFactory.timeOfDayRequestContext();
final TimeOfDayProxy tofdProxy = GWT.create(TimeOfDayProxy.class);
<???>
final Request<Void> setter = context.setCurrentTofD(tofdProxy);
setter.fire(receiver);
...

问题 #1 是我无法设置 tofdProxy 的(不可变的)内容,因为 GWT.create() 只是创建了一个默认构造的代理(即代替“???”?)。Snag #2 是服务器端的“No setter”错误。

有什么魔法可以绕过这些障碍吗?AutoBeanFactory.create() 有一个两个参数的变体,它需要一个对象被一个 autobean 包装——类似的东西会处理 Snag #1(如果 ValueProxys 的 create() 存在这样的事情)。至于 Snag #2,嗯,我敢肯定有很多聪明的方法可以解决这个问题。问题是,有没有在 GWT 中实现过?

4

1 回答 1

0

RequestFactory 需要具有用于客户端到服务器通信的设置器的默认可实例化类。

有一个未决的增强请求,以使用构建器模式添加对不可变类的支持:http ://code.google.com/p/google-web-toolkit/issues/detail?id=5604

于 2011-09-07T01:14:26.230 回答