-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
Suppose we have the following types: MotorizedVehicle, Car extends MotorizedVehicle, MotorCycle extends MotorziedVehicle, and their complementary types MotorizedVehicleDto, CarDto extends MotorizedVehicleDto, MotorCycleDto extends MotorziedVehicleDto.
We also have defined a MotorCycleMapper, a CarMapper and a MotorizedVehicleMapper, each containing methods to map from the entity type to the DTO type.
What I now have in my hand is just a list of MotorizedVehicle instances and I want to map them all to DTOs. But not just to MotorizedVehicleDtos, but to what resembles the most concrete type.
With the current state, I would have to "instanceof" my way through and call the right mapper with the downcasted object myself. Other mapping frameworks allow to define that the actually returned type may be something more special based on the runtime-type of the object to map (in dozer you have to specify such type pairs in the global config).
I wonder if we should support something like that as well. For exampe:
@Mapper( uses = { CarMapper.class, MotorCycleMapper.class } )
public MotorizedVehicleMapper {
@Mapping( withDowncastMapping=true )
public MotorizedVehicleDto vehicleToDto(MotorizedVehicle vehicle);
}would generate to:
public MotorizedVehicleMapperImpl {
private CarMapper carMapper = ...;
@Override
public MotorizedVehicleDto vehicleToDto(MotorizedVehicle vehicle) {
if ( vehicle instanceof Car ) {
return carMapper.carToCarDto( (Car) vehicle );
}
if ( vehicle instanceof MotorCycle ) {
return ...;
}
MotorizedVehicleDto motorizedVehicleDto = new ...
// the usual mapping stuff
}
}I know, usually one could nicely handle such stuff in an object-oriented way by upsmarting the entity types, but, well... you know how it is... ;-)
WDYT?