Replies: 1 comment
-
@jiyeoon00 구현하다 보니깐 repository 단에서는 위에서 정의한 패키지 의존성 방향을 적용하면 구현이 더 어색해지는 부분이 있어서 repository에서는 패키지 의존성 방향 딱히 고려하지 않아도 될 것 같아 예를 들어서 해당 workspace에 속하는 staff를 모두 조회해오고 싶을 때( Case1 select s from Staff s inner join WorkspaceMember wm on s.id = wm.staff.id where wm.workspace.id = :workspaceId
Case2 select wm from WorkspaceMember wm join fetch wm.staff where wm.workspace.id = :workspaceId
애초에 자바에서는 A가 B에 대한 객체 참조를 가지면 A → B 방향만으로의 연관관계가 생기는데(B → A는 불가) DB에서는 A가 B에 대한 FK를 가지면 A → B, B → A 양방향 모두 가능(by. 조인)해서 repository에서는 의존성 방향을 단방향으로 고정하는게 더 이상한 것 같아 결론적으로 repository에서는 위에서 정의한 패키지 의존성 방향 고려하지 말고 상황에 따라 적절한 쿼리를 선택하자 (이 경우에는 Case1) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
패키지 의존성
패키지 간 의존성 사이클을 제거하자
[참고 영상] 우아한 객체지향 - 의존성을 이용해 설계 진화시키기
but 만약 패키지 A와 B 사이에 의존성 사이클이 돈다면 A가 변경되더라도 B가 변경되더라도 서로에게 영향을 미침 → 변경의 범위, 즉 변경의 영향력 매우 커짐
연관성을 판단하는 기준
고려할 질문
간단한 규칙
결론
➡️ 따라서 패키지 의존성 사이클 정도만 없애자! 몇몇 코드를 보면 패키지 간 의존성 사이클이 도는 이유가 JPA 양방향 연관관계에 의한 것도 있지만 API 구현이 잘못된 위치에 있기 때문에 발생하는 경우가 있음. 일단 객체 간 협력 관계(누가 메시지를 보내고 받는지)를 파악하기 좀 힘들 것 같아서 일단은 N → 1 방향으로 패키지 의존성 잡고 가기로 하고, 복잡한 조회 로직을 처리하기 위해 어쩔 수 없이 양방향 연관관계가 필요한 경우도 생길 수 있는데 일단은 아래 그림을 기준으로 구현하기
현재 패키지 간 의존성 상태 (TimecardReq는 제외)
최종 패키지 간 의존성 상태
ex. 현재 workspace 도메인을 보면
StaffWorkspaceController
클래스에서 workspace → schedule로의 패키지 의존성 발생 ⇒ API 구현이 잘못된 위치에 있기 때문, 즉 해당 API 구현은 schedule 도메인 내부에 있어야 함ex.
StaffProfileRepositoryImpl
클래스에서 profile → schedule, profile → workspace로의 패키지 의존성 발생 ⇒ 해당 의존성 발생시키는 메소드findAllByWorkspaceIdAndDateTime
를ScheduleRepositoryImpl
클래스로 옮겨야 함패키지 구조
대략적인 패키지 구조
결론
각 도메인 내부 패키지 구조는 천천히 고려해보고 나중에 한번에 바꾸는 걸로
계층 간 매핑 관계
@Transactional
당 하나의 DB connection이 유지됨 ➡️ 1개의 controller가 여러개의 service 의존하는 경우 하나의 사용자 요청에 대해 여러 번의 DB connection 설정 과정이 필요하게 되는데, 만약 3번의 DB connection 설정이 필요한 상황에서 2번째 connection을 얻지 못하면 응답까지의 시간이 오래 걸림FirebaseCloudMessageService
)는 도메인 service(ex.StaffMessageService
)에 포함되면 안됨 ➡️ 트랜잭션과 무관한 작업이 트랜잭션에 포함되는 것이기 때문에 잘못하면 DB timeout 발생 ➡️ 이 경우에는 1개의 controller → 여러개의 service 의존 가능-Util
클래스 내부에서는 repository 의존(실제로는 service와 비슷한 역할이나 모든 도메인에서 공통적으로 사용, 특히 profile 도메인은 auth 도메인을 제외한 모든 도메인에서 의존) ➡️-Util
클래스는 controller에서 의존X, service에서만 의존 가능결론
1개의 controller → 1개의 service(외부 API 호출하는 service는 여러개 의존 가능) → 여러개의 service(의존성 사이클 유의), 1개의 repository(도메인으로 묶인다면 여러개의 repository도 1개의 repository로 간주 ex.
WorkspaceRepository
,WorkspaceMemberRepository
)Beta Was this translation helpful? Give feedback.
All reactions