mardi 13 avril 2021

Refactoring if statements for DTO

I have too many if statements and I want to refactor my code.

Currently, my code looks like this:

FooDto fooDto = new FooDto();
source.getObjects()
    .stream()
    .forEach(object -> {
        if (object.getName().equals("foo1")) {
            fooDto.setFoo(object.getValue());
        }
        if (object.getNaam().equals("foo2")) {
            fooDto.setNumber(Integer.parseInt(object.getValue()));
        }
        //and a lot of if statements...
    });

The if statements contain string to string mapping. But also string to int and string to LocalDateTime.

How can I refactor this with a design pattern?

Aucun commentaire:

Enregistrer un commentaire