0

I am using POJO to create and fetch data. These POJOs represent our APIs and we use them for testing through REST Assured.

I have a RequestDTO class with 30 variables. Since this is a DTO, I am using 30 setter methods in my class for updating there values.

I am calling these setter methods as below with method chaining. I am using varList variable to read data from csv and supply to this DTO.

However this looks clumsy, less readable & incorrect. I want to know what is a good approach/design pattern here. As I have fairly less knowledge around best practices & design pattern.

Sample code:

    public static void setRequestDTO(List<Object> varList) {
        MyRequestDTO myrequest = new MyRequestDTO()
            .setkey1(varList.get(0).toString())
            .setkey2(varList.get(1).toString())
            // ........
            .setkey30(varList.get(30).toString());
    }
Mandy
  • 433
  • 2
  • 8
  • 25

1 Answers1

1

Firstly, I believe your DTO is too bloated - is there really no other way that you can perhaps break this down into smaller classes?

Secondly, you're using a List<Object> but all of the examples show that you're using String values - is there a chance that you could change the type parameter of the List to eliminate the need for all of the .toString calls?

Thirdly, you are depending heavily on your List containing all of the necessary elements that you're looking to set on your DTO and that they are all in the correct order. This will lead to exceptions being thrown if you have too few elements.

Lastly, while I would consider refactoring this, I'll leave you with one idea that you could proceed with. If you are determined to keep your current DTO structure, then consider putting your List<Object> into a constructor for MyRequestDTO and then performing all of your setters in there. That way you don't have 30 lines of setters whenever you're instantiating a new instance of this DTO and you're only setting these values on instantiation.