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?
Best Answer
If you actually mean something like
and the only function of the mapper is to transform the final result of the getCompany implementation, this seems very unneccesary to me. You could also have
and if you really need to transform the result, a call would look like
getCompany(someid).map(mapper)
instead ofgetCompany(someid, mapper)
in your API design. That transfers the intention much better and removes the often unneccesary clutter to passFunction.identity()
as mapper if you don't want to actually transform the result.