Performance: Nested DTOs
Nested DTOs affect the data serialization and deserialization time during API calls, impacting response time and client rendering speed.
Jump into concepts and practice - no test required
public class OrderDTO { private String customerName; private String customerCity; private List<String> productNames; // getters and setters }
public class OrderDTO { private CustomerDTO customer; private List<ProductDTO> products; // getters and setters } public class CustomerDTO { private AddressDTO address; // getters and setters } public class AddressDTO { private String street; private String city; private CountryDTO country; // getters and setters } public class CountryDTO { private String name; private String code; // getters and setters }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Deeply Nested DTOs | High due to complex data binding | Multiple reflows possible if UI updates per nested data | High paint cost from large DOM updates | [X] Bad |
| Flattened DTOs | Lower DOM operations | Single reflow after data binding | Lower paint cost with simpler DOM | [OK] Good |
Nested DTOs in Spring Boot applications?System.out.println(order.getCustomer().getName()); if order is initialized as below?public class OrderDTO {
private CustomerDTO customer;
public CustomerDTO getCustomer() { return customer; }
public void setCustomer(CustomerDTO customer) { this.customer = customer; }
public static class CustomerDTO {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
}
OrderDTO order = new OrderDTO();
OrderDTO.CustomerDTO cust = new OrderDTO.CustomerDTO();
cust.setName("Alice");
order.setCustomer(cust);public class UserDTO {
private AddressDTO address;
public static class AddressDTO {
private String city;
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
}
public AddressDTO getAddress() { return address; }
public void setAddress(AddressDTO address) { this.address = address; }
}
UserDTO user = new UserDTO();
user.getAddress().setCity("Paris");OrderDTO contains a list of ItemDTO objects. You want to convert this nested DTO into a flat list of item names using Java streams. Which code snippet correctly achieves this?public class OrderDTO {
private List<ItemDTO> items;
public List<ItemDTO> getItems() { return items; }
public void setItems(List<ItemDTO> items) { this.items = items; }
public static class ItemDTO {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
}
OrderDTO order = ...; // initialized with items