Search code examples
javagwtrequestfactorygwt-2.4

GWT - how to implement RequestContext methods outside an @Entity-annotated class?


Is it possible to implement RequestContext methods outside an @Entity-annotated class?

@Entity
class TheEntity {
  public static TheEntity theMethod() { ... } // don't want it here
}

@Service(TheEntity.class)
interface TheEntityRequest extends RequestContext {
  Request<TheEntity> theMethod(); // this one
}

Solution

  • Yes, you can. This is mentioned in the official GWT documentation, though it's not very detailed.

    I've found of great help this blog post by David Chandler.

    Some pointers:
    (example links are from the project discussed in the blog post)

    Entity locator methods (find, create, getId, getVersion) can be moved in a generic Locator class (example). For this to work your entities must extend a BasicEntity class that has the getId and getVersion methods. Then on the client you would specify the locator like this:

    @ProxyFor(value = MyEntity.class, locator = GenericLocator.class)
    public interface MyEntityProxy extends EntityProxy {
    ...
    }
    


    Data access methods can be moved in a service. You can have a generic service (example), and then extend it for each entity to provide specific methods (example).

    On the client you define your service like this:

    // 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);
    ... 
    }
    

    Note the need for a service locator also. It can be as simple as this.