GWT RF:如何在客户端和服务器上共享相同的代码 [英] GWT RF: How to share the same code in client and server

查看:101
本文介绍了GWT RF:如何在客户端和服务器上共享相同的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用相同的代码对客户端和服务器端的对象进行排序和操作。



但是我面临一个问题,因为在客户端,我们需要代表服务器类的代理接口。



有两种方法可以在两者中使用相同的接口吗?我知道RF有一种机制,可以通过线路将bean属性从服务器实例复制到客户端实例。

解决方案在客户端和服务器中共享代码的唯一方法是在双方实现相同的接口并在共享代码中使用它。

由于RF按照您在查询中所说的从服务器向客户端复制属性,理论上我们可以在两边使用相同的接口(代理服务器)(简单代码),设置@ValueFor值指向自己。



让我们看一个例子:

 @ProxyFor(Foo.class)
接口Foo扩展ValueProxy {
String getBar();
}

//服务器端实现
class FooImpl实现Foo {
String getBar(){returnbar;};
}

作为信息,我们在产品中使用这种方法,因此我们可以销售2个后端解决方案(一个基于GAE,另一个基于couchdb)。

上面的代码适用于客户端代码,它不会创建新值,但是如果您想创建他们,定义一个值定位器就足够了:

  //说出RF使用哪个定位器在服务器端创建类
@ProxyFor(value = Foo.class,locator ALocator.class)
interface Foo extends ValueProxy {
}

public class ALocator extends定位器< Foo,String> {
public Foo create(Class< ;? extends Foo> clazz){
return new FooImpl();
}
...
}

不处理服务器端的接口,请参阅以下问题: 7509 5762



但是,正如您可以在问题评论中看到的,已经有一个修复此(待审核)。希望它会被包含在GWT的下一个版本中。



与此同时,您可以使用这种方法,只需复制文件 ResolverServiceLayer.java 在您的src文件夹中并应用此补丁

I would like to use the same code to sort and manipulate objects in client and server sides.

But I am facing a problem since in client we need a proxy interface representing the class of the server.

Is there a way to use the same interface in both?, I know RF has a mechanism to copy bean attributes from the server instance to the client instance when it is sent through the wire.

解决方案

As Thomas says in his answer, the only way in current GWT to have shared code in client and sever is implementing the same interface in both sides and using it in your shared code.

Since RF copies attributes from the server to the client as you say in your query, in theory we could use the same interface (the proxy one) in both sides (simpler code), setting the @ValueFor value pointing to itself.

Lets see an example:

 // Shared interface in client and server sides
 @ProxyFor(Foo.class)
 interface Foo extends ValueProxy {
    String getBar();
 }

 // Server side implementation
 class FooImpl implements Foo {
    String getBar(){return "bar";};
 }

As information, we use this approach in our product, so as we can sell 2 backend solutions (one is based on GAE and other on couchdb).

The code above works for client code which does not create new values, but if you want to create them, it is enough to define a value locator:

 // Say RF which locator to use to create classes in server side
 @ProxyFor(value = Foo.class, locator ALocator.class)
 interface Foo extends ValueProxy {
 }

 public class ALocator extends Locator<Foo, String>  {
   public Foo create(Class<? extends Foo> clazz) {
    return new FooImpl();
   }
   ...
 }

Unfortunately, RF does not deal with interfaces in the server side see issues: 7509 and 5762.

But, as you can read in the issues comments, there is already a fix for this (pending for review). Hopefully it would be included in a next release of GWT.

In the meanwhile, you can use this approach, just copying the file ResolverServiceLayer.java in your src folder and applying this patch to it.

这篇关于GWT RF:如何在客户端和服务器上共享相同的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆