강의

멘토링

로드맵

Inflearn brand logo image

인프런 커뮤니티 질문&답변

흔들거북이님의 프로필 이미지
흔들거북이

작성한 질문수

[코드팩토리] [초급] NestJS REST API 백엔드 완전 정복 마스터 클래스 - NestJS Core

여러개의 파이프 동시에 사용하기

직접 생성해서 사용하는 pipe class에 대한 질문입니다.

작성

·

52

·

수정됨

0

안녕하세요!

 

DefaultValuePipe와 이번 강의에 나온 인자를 받는 Custom Pipe Class는 반드시 인자를 받아야 하는 구조이기 때문에 무조건 직접 생성해서 사용해야한다. 라고 이해하였는데 맞을까요?

 

또한, 강의에서 MaxLengthPipe와 MinLengthPipe 각각 @Injectable() 데코레이터를 붙여서 작성하셨는데, 직접 생성해서 사용하는거라면 굳이 필요없지 않을까 생각하여서 빼고 테스트 해보니 문제없이 잘 동작하는 것 같습니다. 특별히 붙여야만 하는 이유가 있는걸까요?!

 

 

답변 2

1

코드팩토리님의 프로필 이미지
코드팩토리
지식공유자

안녕하세요!

1) 인자를 받아야한다면 말씀하신게 맞습니다. (Custom Pipe예제가 아니라도 모두 해당됩니다)

2) Injectable 데코레이터는 IoC 컨테이너가 의존성 주입으로 인지 할 수 있도록 해주는 기능입니다. 만약에 '직접' 인스턴스화를 하고 있다면 필요 없는 기능입니다. (모듈의 providers 파라미터에 인스턴스화 하지 않고 클래스를 직접 주입해주는 것과 같습니다)

감사합니다!

언제나 빠른 답변 감사드립니다~! 잘 이해하였습니다.

0

안녕하세요, 인프런 AI 인턴입니다.

흔들거북이님, Directly creating and using Pipe classes like DefaultValuePipe and Custom Pipe classes in NestJS generally requires instantiation (new) when they need constructor arguments. Your understanding is correct if the Pipe requires arguments to be configured for specific use cases. Direct instantiation allows you to pass these configurations directly.

As for using @Injectable() for Pipes like MaxLengthPipe and MinLengthPipe, while it’s true that you might not strictly need @Injectable() if you are directly instantiating them, the decorator serves a purpose:

  1. Singleton Management: When decorated with @Injectable(), the NestJS Dependency Injection container can manage them, meaning they’ll be instantiated and shared as singletons wherever used, unless manually instantiated.

  2. Consistency and Scalability: Applying @Injectable() ensures consistency across your codebase, making it easier if later you decide to let the DI system manage these instances instead.

  3. Future-proofing: If your application grows or the Pipe logic becomes complex or starts depending on other services, having it as an injectable service becomes beneficial.

For more detailed information on this topic, you might find it useful to explore the NestJS documentation on Pipes. This will provide you with a broad overview and specific use cases on how NestJS handles dependency injection.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

흔들거북이님의 프로필 이미지
흔들거북이

작성한 질문수

질문하기