I have a problem with the structure of my API.
My API includes the study domain class, which has an educationLevelId attribute; This attribute belongs to the academicLevel domain class.
Study Domain - Domain Layer
public class Study {
private final Long id;
private final Long employeeId;
private Long educationLevelId;
//Other attributes and methods
}
AcadmicLevel Domain - Domain Layer
public class educationLevel {
private final Long id;
//Other attributes and methods
}
public class AcademicLevel {
private final Long id;
private List<educationLevel> educationLevels;
//Other attributes and methods
}
In my Study domain there is a findStudiesByEmployeeIdUseCase, which requires to return a StudyDTO.
Study Domain - Application layer
public class StudyDTO {
private Long id;
private Long employeeId;
private Long educationLevelId;
private Long academicLevelId;
//Other attributes
}
StudyDTO requires the academicLevelId, which is an attribute of another domain class. To address this, I created the shared service FindAcademicLevelIdsByEducationLevelIdsService (interface) and its implementation is located in the application layer of the academicLevel domain.
Shared Domain - Application layer
public class AcademicAndEducationLevelIdDTO {
private Long academicLevelId;
private Long educationLevelId;
//Getters
}
public interface FindAcademicLevelIdsByEducationLevelIdsService {
List<AcademicAndEducationLevelIdDTO> execute(List<Long> educationLevelIds)
}
The problem is that the implementation uses the AcademicLevelRepository (an interface of domain layer), which shouldn't depend on the application layer according to Hexagonal Architecture principles. So I am not sure which approach to take.
Option 1:
<R> List<R> findAcademicLevelIdsBy(List<Long> educationLevelIds, Class<R> clazz)
In this option, the repository implementation would return a List of results according to the specified class type (Using entityManager). This way, the AcademicLevelRepository would not depend on the application layer.
Option 2:
In the second option, I would create a package in the domain layer containing all internal DTOs. The repository woulr return an internal DTO, which would then be mapped to AcademicAndEducationLevelIdDTO. I don't like this option because according to Hexagonal Architecture principles, the DTOs components belong to the application layer.
If there are other options, please tell me.
I need help with another options or helping on to take the better option.