vendredi 12 octobre 2018

Use Generics/Patterns in Java for object mapping

I have a proxy service which exposes several APIs - each requesting an Object of a specific type and returning an Object of a specific type. Requests come to my ProxyService which are mapped to the format expected by the underlying service:

public class ProxyService {
    public ProxyServiceResponse_A performA(ProxyServiceRequest_A request);
    public ProxyServiceResponse_B performB(ProxyServiceRequest_B request);
    public ProxyServiceResponse_C performC(ProxyServiceRequest_C request);
    ...
}

public class ProxiedService {
    public ProxiedServiceResponse_A performA(ProxiedServiceRequest_A request);
    public ProxiedServiceResponse_B performB(ProxiedServiceRequest_B request);
    public ProxiedServiceResponse_C performC(ProxiedServiceRequest_C request);
    ...
}

I want to create generic mappers for mapping incoming ProxyService requests to ProxiedService requests:

public class MyRequestMapper {
    public ProxiedServiceRequest_A mapRequestA(ProxyServiceRequest_A request);
    ...
}

I know of dozer and apache-velocity but I don't want to use those, instead do this on my own. Is there a design pattern or a generic way to achieve this?

Aucun commentaire:

Enregistrer un commentaire