QnA

dependency-rules

Q&A 정리: dependency-rules

단방향 의존성 규칙이 왜 중요하고, 위반하면 어떤 일이 발생하는가?

한 모듈이 같은 층이나 상위 층의 모듈을 참조하면 안 된다는 규칙이다. 이를 지키면 한 부분을 수정해도 다른 부분에 예상치 못한 영향을 주지 않아, 안전하게 독립적으로 코드를 변경할 수 있다.

A module on one layer cannot use other modules on the same layer, or the layers above.

The key difference of Feature-Sliced Design from an unregulated code structure is that pages cannot reference each other.

This allows you to make isolated modifications without unforeseen consequences to the rest of the app.


같은 레이어 내에서 슬라이스끼리 참조가 금지된 이유는?

같은 레이어의 슬라이스끼리 서로 참조하면 얽히고설켜 독립성이 무너진다. 엔티티 간 상호작용이 필요하면 상위 레이어(Feature, Page)에서 조합하여 높은 응집도와 낮은 결합도를 유지한다.

Slices cannot use other slices on the same layer, and that helps with high cohesion and low coupling.

Entities in FSD are slices, and by default, slices cannot know about each other.

In real life, however, entities often interact with each other, and sometimes one entity owns or contains other entities.

Because of that, the business logic of these interactions is preferably kept in higher layers, like Features or Pages.


FSD에서 같은 레이어의 엔티티끼리 타입을 참조해야 할 때 어떻게 하는가?

타입에 제네릭 슬롯을 두어 외부에서 연결을 주입하거나, 교차 참조 전용 공개 API를 별도로 만든다. 연결을 명시적으로 선언함으로써 의존성을 투명하게 관리하고 도메인 분리를 유지할 수 있다.

You can make your types accept type arguments as slots for connections with other entities, and even impose constraints on those slots.

To make cross-imports between entities in FSD, you can use a special public API specifically for each slice that will be cross-importing.

By making explicit connections between entities, we stay on top of inter-dependencies and maintain a decent level of domain separation.


FSD에서 인증 토큰을 Entities에 저장하면 어떤 아키텍처 문제가 발생하고, 어떻게 해결하는가?

인증 토큰을 Entities 레이어에 두면, 다른 엔티티가 이를 참조할 때 레이어 규칙을 위반하게 된다. 해결 방법으로는 요청마다 토큰을 직접 전달하거나, 전역 저장소(localStorage, Context)에 노출하거나, API 클라이언트에 토큰을 주입하는 방식이 있다.

Since the API client is usually defined in shared/api or spreaded across the entities, the main challenge to this approach is making the token available to other requests that need it without breaking the import rule on layers: A module (file) in a slice can only import other slices when they are located on layers strictly below.

There are several solutions to this challenge:

  • Pass the token manually every time you make a request
  • Expose the token to the entire app with a context or a global store like localStorage
  • Inject the token into the API client every time it changes