开发者

Sending a ValueProxy for an immutable class upstream to server

开发者 https://www.devze.com 2023-04-03 04:33 出处:网络
Suppose I am trying to use GWT\'s RequestFactory to pass an immutable type between client and server, bidirectionally.Let\'s say the underlying type is TimeOfDay, which is designed to be immutable:

Suppose I am trying to use GWT's RequestFactory to pass an immutable type between client and server, bidirectionally. Let's say the underlying type is TimeOfDay, which is designed to be immutable:

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

  public TimeOfDay(final int hours, final int minut开发者_JAVA技巧es) {...}
  public int getHours() {...}
  public int getMinutes() {...}
}

I can proxy this class with a ValueProxy:

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

Now, I can quite easily create TimeOfDay instances on the server side and return them to the client, via this on server side:

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

...and this on the client side:

@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);
...

This works great. But, if I try this in the reverse direction I hit snags. I.e., on server side:

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

...and on client side:

@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);
...

Snag #1 is that I have no way to set the (immutable) contents of tofdProxy, since GWT.create() just makes a default constructed proxy (i.e. what goes in place of "???"?). Snag #2 is the "No setter" error on the server side.

Is there some magic to circumvent these snags? AutoBeanFactory.create() has a two-argument variant which takes an object to be wrapped by an autobean --- something like that would take care of Snag #1 (if such a thing existed for create() of ValueProxys). As for Snag #2, well, I'm sure there are many clever approaches to deal with the issue; the question is, have any been implemented yet in GWT?


RequestFactory requires default-instantiable classes with setters for client-to-server communications.

There's a pending request for enhancement to add support for immutable classes, using a builder pattern: http://code.google.com/p/google-web-toolkit/issues/detail?id=5604

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号