Search code examples
gwtgwt-rpcrequestfactory

GWT RequestFactory, how to implement Query results DTOs


I have a large implementation of GWT-RPC and actually I'm evaluating the alternative to move to RequestFactory. The basic reason why I'm doing that is because I'm not very happy with TypeSerializers solution that GWT-RPC produce, and the huge size of code generated for serialize/deserialize that actually represent in my case more than 60% of the whole JS resulting code.

So for a week I have been reading all about requestFactory and my first impression was that the API is confined to the management of persistent Entities, but it doesn't shows clearly how the API will support Query Results Proxys.

I read that it could be done by using ValueProxy but I couldn't found any good example of doing that. What I mean is asume that I need to provide a Sales Ranking of Top 10 clients for last month. Information like that is easy to provide via RPC, but with RequestFactory I'm not sure. I don't have any Domain Object to proxy.

via GWT-RPC I will have a service method like:

List<ClientRankingDTO> getClientRanking(String clientCode, Date fromDate, Date untilDate);

My ClientRankingDTO will looks like:

public class ClientRankingDTO implements Serializable {

    private String clientCode;
    private String clientDescription;
    private Integer rankingPosition;
    private BigDecimal amount;
    // Getters and setters are hidden for simplicity
}

So at my DAO layer I will have some method over the SalesStatistics Domain Model Entity that will calculate the corresponding ranking and will generates the List of ClientRankingDTO with the corresponding results.

That could be done by a simple query like:

Select top 10 client_code, sum(amount) from sales_stats A 
       where A.sales_date>=fromDate 
         and A.sales_date<=untilDate 
group by client_code
order by amount desc

implemented with the ORM of your choice.

My question is how can I implement this kind of service with RequestFactory?


Solution

  • Simply use ValueProxy instead of EntityProxy, and you turn RequestFactory into a "simple RPC" mechanism, similar to GWT-RPC.

    Proxy:

    @ProxyFor(ClientRankingDTO.class)
    interface ClientRankingProxy extends ValueProxy {
      // getters for the properties, no need for setters if it's only server-to-client
    }
    

    Then, in the RequestContext (client-side):

    Request<ClientRankingProxy> getClientRanking(String clientCode, Date fromDate, Date untilDate);
    

    And in the service on the server-side:

    public ClientRankingDTO getClientRanking(String clientCode, Date fromDate, Date untilDate) {
      // your request to the database, mapping the result to ClientRankingDTO
    }