0

I am working on a GWT project and are a bit confused. Its a server/client project. I am handling the json with Autobean. Autobean needs to use interfaces to work. I haven't work so much with interfaces before and need some help. This is how the Autobean code:

interface User {
      int id();
      void setId(int id);
      String getUsername();
      void setUsername(String username);
    }

interface Result{
    List<User> getUsers();
    void setUsers(List<User> users);
}


interface Beanery extends AutoBeanFactory{
      AutoBean<User> makeBean();
      AutoBean<Result> makeBeans();
      }

To convert the json to the a List I do this.

AutoBean<Result> autoBeanCloneAB = AutoBeanCodex.decode(beanFactory, Result.class, "{\"users\": " + json + "}" );
List<User> users = autoBeanCloneAB.as().getUsers();

I am also having a User class, and I am unsure if this is unnecessary or how I should be handling this.

public class User {

private int id;
private String username;


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}
}

I have in all my earlier projects only been working with model classes and not interfaces. I have tried to read and seem to understand the concept of an interface, but in this case i cannot see any reason to use the User class when I have the User interface. What is the right approach? Is there any purpose of using a the User class, and if yes, please explain.

As usual. All help is appreciated

MDK
  • 95
  • 1
  • 1
  • 8

1 Answers1

1

The interface is needed if you are using a Proxy which is the foundation of Autobean. It works very much like java proxy pattern. The interface will actually be a proxy to the concrete implementation without having to have the compile the concrete implementation. In previous revisions of GWT the rpc mechanism was used and this create problem because the server side classes had to be 100% GWT compatible, and serializable to be compiled. The AutoBean and GWT implementation of RequestFactory get around this by simply creating a "DTO" from your interface during compile time. This allows you to invoke getter/setter methods on the concrete implementation without having to have the concrete implemenation compiled by gwt. You can simply do a state transfer from the server side to the client side via json and the getter/setter methods will be invoked on the client side proxy (generated interface implementation). The interface seems like overhead but it is required in order to abstract away the code in the server side classes for example.

I would recommend looking into gwt requestfactory as it hides / handles a lot of the Autobean boilerplate for you. You can create a Bean using the RequestFactory and still convert it to json using the ProxyUtils class ( If I remember correctly). Then you would simply create an interface and annotate it with the @ProxyFor(concreteImplementation.class). The compiler will generate all the necessary code for you and you don't have to mess with create AutoBeanFactory interfaces and such.

Hope this helps.

P.S.

The other option is to use the jsni native interface but this comes with a few subtle nuances of it's own.

Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
  • Thanks. This cleared thinks. I think i will stick with my Autobean and interface – MDK Dec 01 '15 at 11:45