220913_광고 관리 플랫폼 대행사 센터 제작 18_리팩토링 : DTO, Response

창고·2022년 9월 13일
0

해당 게시글은 개인 프로젝트인 "광고 관리 플랫폼 대행사 센터 제작"
#57 "리팩토링 : DTO, Response 수정" 이슈를 다루고 있습니다.

1. 리팩토링 진행하려 하였으나..

  • AgentDto, AgentGroupDto의 from 메소드에서 순환 참조로 인해
    StackOverflowError 발생
  • 문제 코드
    public static AgentGroupDto from(AgentGroup entity) {
        return new AgentGroupDto(
                AgencyDto.from(entity.getAgency()),
				entity.getAgents().stream()
                        .map(AgentDto::from)
						.collect(Collectors.toCollection(LinkedHashSet::new)),
				entity.getId(),
                entity.getName(),
                entity.getCreatedAt(),
                entity.getCreatedBy(),
                entity.getModifiedAt(),
                entity.getModifiedBy()
        );
    public static AgentDto from(Agent entity) {
        return new AgentDto(
                AgencyDto.from(entity.getAgency()),
                AgentGroupDto.from(entity.getAgentGroup()),
                entity.getUserId(),
                entity.getUserPassword(),
                entity.getNickname(),
                entity.getEmail(),
                entity.getCreatedAt(),
                entity.getCreatedBy(),
                entity.getModifiedAt(),
                entity.getModifiedBy()
        );
    }
  • 양방향 관계이기 때문에 에이전트 그룹과 에이전트 간의 매핑 관계를 DTO로 처리한 AgentGroupWithAgentsDto에서는 문제가 발생하지 않았으나 이를 삭제하고 AgentGroupDto를 수정하니 from 메소드의 get 에서 순환참조 발생 중
  • 각 엔티티에서 @ToString.Exclude 처리를 이미 진행하였으나 수정되지 않아 @Getter 쪽 문제로 보임
  • 단방향 관계로 변경하기엔 양방향으로 매핑이 되어야 하는 부분이 있어서 쉽게 변경은 어려울 것으로 보임... 그렇다고 어느 한 쪽에서 엔티티가 아닌 ID / 이름값만 가지고 있기엔 뭔가 추가로 구현해야 할 것들이 많아 보임
  • 쉽게 리팩토링할 사안은 아닌 것으로 보여 일단 해당 이슈 종료 처리 후 개선점 모색 필요
profile
공부했던 내용들을 모아둔 창고입니다.

0개의 댓글