Recently, we have started to migrate a spring application to java 8. The application is divided into 3 layers, rest controllers, service and the repository layer. Our 'architect' proposed that our service layer, always accept a mapper function e.g.
public <T> Optional<T> getCompany(int companyId, Mapper<S,T> mapper)
His reason is flexibility - to allow the calling class to format the result in whatever or it wish to.
I do disagree with the the proposal, as i think it's just abuse of the feature. At the same time, i dont want to give the impression that im not willing to adapt new features / concepts if they really bring benefit.
My question is, is the above proposal valid or just an abuse of a java 8 feature?
Mapper
function? i.e. is your objection to using a Java 8 feature or to the whole idea - because your response in each case needs to be very different, and objecting to aMapper
when what you're really objecting to is over-engineering will be at best a waste of time.S
, and if the caller needs aT
, then he/she can map the result using aMapper<S, T>
? What happens when you do not need to map? (Presumably, you must pass the identity function...) Having the caller code (optionally) apply the mapping is just as flexible as your architect's proposed solution.