Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR의 목적이 무엇인가요?
지속 성장 가능한 소프트웨어를 위한 구조 개선 예시 코드 작성
이슈 ID는 무엇인가요?
설명
Service란 무엇인가?
Service는 본래 DDD(Domain-Driven Design) 이라는 개발 철학에서 제시한 개념이다. 그리고 서비스의 본질은 “비즈니스 서비스의 파사드” 라고 한다. 즉 도메인 로직들의 세부 구현이 드러나는 것이 아니라 그 자체로 비즈니스 흐름이 보여야 한다.
따라서 서비스 코드의 전체적인 형태는 다음과 같아야 한다.
적용 예시
findMoimRole
위 코드를 분석한다면 크게 다음과 같은 비즈니스 흐름을 가진다.
위에서 볼드체로 표기된 부분이 서비스코드에서 드러나야하는 비즈니스 흐름이며 하위 항목들이 세부 구현이라 할 수 있다. 따라서 우리는 비즈니스로직은 business layer 에 구현로직은 implement layer에 작성하도록 한다. 개선한 코드는 아래와 같다.
이때 주의해야하는 것은
ChamyoValidator
와 같이 협력하는 구현객체가 존재할 때 응집도에 대한 고민을 충분히 한 후에 배치하도록 한다. 가령 검증이 비즈니스 흐름에서 드러나야 할 정도로 중요한 로직이라면 Service 클래스에 배치하거나 모든 찾는 행위에 검증이 필요하다면 find 내부에 배치하는 등 충분한 고민 후에 적용한 후 문서로 팀원들에게 전달해야한다.또 필요하다면 새로운 도메인 객체를 고려한다.
가격을 계산하는 역할은 누구의 것인가?
셋 모두 애매한 부분이 있다고 느껴진다면 아래와 같이 개선한다.
위 경우 Manager는 사실 이름만 바꾸는 것으로 도메인으로 취급할 수 있다.
무엇이 더 자연스러운지는 케이스 별로 다를 수 있으므로 본인 판단하에 수정후 팀원들과 상의한다.
가이드 정리
레퍼런스
질문 혹은 공유 사항 (Optional)