I develop application which contains few tiers. We have DAO layer which returns model objects. We also have mappers which instantiate DTO objects and send them to clients. Entities are mapped to DTOs in Controller layer. I've introduced inheritance in few entity classes. Let's assume sth like on image below
class diagram (not enough reputation points to past image directly)
I ask DAO for list of animals from the concrete ZOO. Then I get list List animals, but they are of concrete type because Animal is abstract and we cannot have just Animal in the database. I would like to create DTOs from this model objects. I have to use mapper in which I have if .. else statements checking type of each animal and then creating proper DTO, sth like
if (animal instanceof Dog) {
.. create dog dto
} else if (animal instance of Cat) {
.. create cat dto
} .. and so on
This code does not look nice. It would be nice to use polymorphism and call some method on each animal to produce DTO, but it is bad to have logic in domain model creating DTO objects just to communication. How do you resolve such situations?
Edit: To be more specific, I want to have DTO like 1. DogDTO which contains only fields color and name 2. FishDTO which contains only numberOfFins Not one big AnimalDTO with all possible attributes