【问题标题】:GWT - how to implement RequestContext methods outside an @Entity-annotated class?GWT - 如何在 @Entity 注释的类之外实现 RequestContext 方法?
【发布时间】:2011-10-04 19:58:57
【问题描述】:
是否可以在 @Entity 注释的类之外实现 RequestContext 方法?
@Entity
class TheEntity {
public static TheEntity theMethod() { ... } // don't want it here
}
@Service(TheEntity.class)
interface TheEntityRequest extends RequestContext {
Request<TheEntity> theMethod(); // this one
}
【问题讨论】:
标签:
java
gwt
requestfactory
gwt-2.4
【解决方案1】:
是的,你可以。这个在官方GWT documentation里有提到,虽然不是很详细。
David Chandler 的 blog post 对我有很大帮助。
一些建议:
(示例链接来自博文中讨论的项目)
实体定位器方法(find、create、getId、getVersion)可以在通用定位器类 (example) 中移动。为此,您的实体必须扩展具有 getId 和 getVersion 方法的 BasicEntity 类。然后在客户端上,您可以像这样指定定位器:
@ProxyFor(value = MyEntity.class, locator = GenericLocator.class)
public interface MyEntityProxy extends EntityProxy {
...
}
数据访问方法可以在服务中移动。您可以拥有一个通用服务 (example),然后为每个实体扩展它以提供特定方法 (example)。
在客户端上,您可以这样定义服务:
// MyEntityDao is your server service for MyEntity
@Service(value = MyEntityDao.class, locator = MyServiceLocator.class)
interface MyEntityRequestContext extends RequestContext {
Request<List<MyEntityProxy>> listAll();
Request<Void> save(MyEntityProxy entity);
...
}
请注意还需要一个服务定位器。它可以像this 一样简单。