Archive for October, 2011

October 1, 2011

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