mercredi 21 août 2019

Java: How to neatly create a data mapper utility for 200 fields not to repeat same checks again all over?

Suppose we have scenario like following

if (!areEquals(empFraMD.getUserId(), empFraBubba.getUserId())) {
            empFraMD.setUserId(empFraBubba.getUserId());
            updated = true;
} 

if (!areEquals(empFraMD.getFirstName(), empFraBubba.getFirstName())){
            empFraMD.setFirstName(empFraBubba.getFirstName());
            updated = true;
}
 .........200 Times check for 200 different parameter. 150 Times String checks 20 times integer checks. 
 //EmpFraMD is current employees in Database and empFraBubba is employees fetched from SFTP 
    and both are objects of same class. The requirement here is just to update database 
    only in case difference

updated flag is just to set at end of code that if updated is true then setLastUpdated(now) in database.

Is there neat way to do this in Java using either design pattern or beans or some other way of transformation rather then write same chunk again and again?

Aucun commentaire:

Enregistrer un commentaire