vendredi 21 juin 2019

Design: best practice to convert REST dtos to java database entities

I'm working on an Android project. I get some DTO from REST API (e.g. CompanyDTO, DepartmentDTO,ProjectDTO) , and I want to persist them in my database.

These DTO may have has-a relations: for example, in each CompanyDTO we have a list of DepartmentDTO.

So I already created all converters that convert DTO to database entities (CompanyEntity, DepartmentEntity, etc.). But I have a problem, for example:

for (CompanyDTO companyDTO : apiDTOS.getCompanyDTO()) {
   CompanyEntity companyEntity = CompanyConverter.convert(companyDTO);
   companyRepository.insert(companyEntity);
   for (DepartmentDTO departmentDTO : companyDTO.getDepartmentDTO) {
      DepartmentEntity departmentEntity = DepartmentConverter.convert(departmentDTO);
      departmentRepository.insert(departmentEntity );

    // here we have more imbrications
   }
}

I don't think it's good in the view of design, I want to know if there is a best practice to do this kind of conversions ? Thank you.

Aucun commentaire:

Enregistrer un commentaire