lundi 31 août 2020

Good Design for if else object mapping

I have this logic:

public void method(Boo result, Foo foo, Bar bar) {
   if(foo != null) {
      if(foo.getId() != null){
         result.setId(foo.getId());
      } else {
         result.setId(bar.setId());
      }
      if(foo.getName() != null){
         boo.setName(foo.getName());
      } else {
         result.setName(bar.setName());
      }
      // and many similar attributes
   } else {
      result.setId(bar.setId());
      result.setName(bar.setName());
      // and many similar attributes
   }
}

I find this way ugly, is there any way to make it in better design. I know that is better to use mapstruct, but in this project I can't.

Aucun commentaire:

Enregistrer un commentaire