假设我正在尝试使用GWT的RequestFactory双向传递客户端和服务器之间的不可变类型。比方说,基础类型是的TimeOfDay,它被设计为不可变的:向服务器上游的不可变类发送ValueProxy
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的(immutable)的内容,因为GWT。 create()只是构造一个默认的构造代理(即代替“???”?)的代码。 Snag#2是服务器端的“No setter”错误。
是否有一些魔法来规避这些障碍? AutoBeanFactory.create()有一个两个参数的变体,它需要一个对象被自动包装 - 类似的东西会照顾到Snag#1(如果ValueProxys的create()存在这样的事情)。至于Snag#2,好吧,我相信有很多聪明的方法来处理这个问题。问题是,GWT中有没有实现过?