this is more a theoretical question than a practical one.
We have a layered architecture, sort of:
UI <--DTO1--> UI_JavaHandler <--DTO2--> Backend
DTO1 needs to have a bit more data than DTO2, and precisely an extra String. So the question is, should DTO1 extend DTO2 or should it encapsulate DTO2? In the first case, the code in UI_JavaHandler will be:
public void acceptAction(DTO1 dto1) {
//do something with dto1.getString();
backend.call(dto1);
}
Whereas in the second case:
public void acceptAction(DTO1 dto1) {
//do something with dto1.getString();
backend.call(dto1.getDto2());
}
Extending a Java DTO2 works fine, but I don't like the idea of using extension just for adding new data. I am used to using extension for adding new behaviour (like Animal being extended by Dog and Cat). Since the same effect can be used by aggregation we should not use extension, but I have no strong arguments against such (ab)use(?).
On the other hand, I may be completely wrong.
What is your take on this? Thank you.