So here's my conundrum. I am programming a tool that needs to work on old versions of our application. I have the code to the application, but can not alter any of the classes. To pull information out of our database, I have a DTO of sorts that is populated by Hibernate. It consumes a data object for version 1.0 of our app, cleverly named DataObject. Below is the DTO class.
public class MyDTO {
private MyWrapperClass wrapper;
public MyDTO(DataObject data) {
wrapper = new MyWrapperClass(data);
}
}
The DTO is instantiated through a Hibernate query as follows:
select new com.foo.bar.MyDTO(t1.data) from mytable t1
Now, a little logic is needed on top of the data object, so I made a wrapper class for it. Note the DTO stores an instance of the wrapper class, not the original data object.
public class MyWrapperClass {
private DataObject data;
public MyWrapperClass(DataObject data) {
this.data = data;
}
public String doSomethingImportant() { ... version-specific logic ... }
}
This works well until I need to work on version 2.0 of our application. Now DataObject in the two versions are very similar, but not the same. This resulted in different sub classes of MyWrapperClass, which implement their own version-specific doSomethingImportant(). Still doing okay. But how does myDTO instantiate the appropriate version-specific MyWrapperClass? Hibernate is in turn instantiating MyDTO, so it's not like I can @Autowire a dependency in Spring.
I would love to reuse MyDTO (and my dozens of other DTOs) for both versions of the tool, without having to duplicate the class. Don't repeat yourself, and all that. I'm sure there's a very simple pattern I'm missing that would help this. Any suggestions?