Polymorphic entities with GWT 2.4

by Stefan

GWT 2.4 finally adds polymorphism support to the RequestFactory infrastructure (detailed description). Here’s a small example on how to use it.

The scenario is well known from your first OO lesson. A basic class Vehicle is extended by the sub classes Car and Truck. Our back-end service layer allows clients to load a list of vehicles:

public class VehicleProvider {
  public List<Vehicle> getListOfVehicles() {
    return Arrays.asList( new Vehicle(), new Car(), new Truck() );
  }
}

To enable to RequestFactory logic to “know” all subclasses for Vehicle, use the new @ExtraType annotation to reference all known sub-types:

import java.util.List;

import cleancodematters.requestfactory.polymorphism.server.VehicleProvider;

import com.google.web.bindery.requestfactory.shared.ExtraTypes;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.Service;

@Service(value=VehicleProvider.class)
// Reference CarProxy and TruckProxy which extend VehicleProxy
@ExtraTypes( {CarProxy.class, TruckProxy.class} )
public interface VehicleRequestContext extends RequestContext {
  Request<List<VehicleProxy>> getListOfVehicles();
}

That’s it. You can find the full example on github.

Advertisements

One Comment to “Polymorphic entities with GWT 2.4”

  1. This is my first time go to see at here and i am truly happy to read all at alone place.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: