vendredi 28 juillet 2023

Abstract classes in DTO - complex dependency beetwen layers

I have a following problem. I have two classes: AnimalService responsible for domain logic and AnimalController which is responsible for public API available to clients. AnimalServices uses domain abstract class Animal and AnimalController uses DTO objects. AnimalService is not aware of AnimalController existence, so Animal class does not know about AnimalDTO.

public abstract class Animal {
   ...
}



public class AnimalController {

   private final AnimalMapper animalMapper;

   ...
   
   public AnimalDTO createAnimal(AnimalDTO newAnimal) {
      Animal domainAnimal = animalMapper.fromDTO(newAnimal);
      Animal createdAnimal = animalService.createAnimal(domainAnimal);
      return animalMapper.toDTO(createdAnimal);
   }
}




public class AnimalService {
   
   public Animal createAnimal(Animal domainAnimal) {
      ...
   }
}

Since the Animal class may have many implementations, Im having troubles with designing AnimalDTO. Creating a new DTO for every new implementation of Animal seems like a very bad dependency, which requires changes in multiple places each and every time and a Single Responsibility principle violation.

Is there any better solution / design that I could possibly use in that case?

Aucun commentaire:

Enregistrer un commentaire