feat: NestJS 핵심 개념 학습 #3
Merged
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.
NestJS 핵심 개념
NestJS의 핵심 개념인 IoC, 의존성 주입, 그리고 아키텍처 구조를 학습하고 실제 Post CRUD API를 구현했습니다.
📌 학습 내용
1. IoC (Inversion of Control, 제어의 역전)
IoC란?
IoC가 아닌 패턴 (전통적인 방식)
문제점:
IoC 패턴 (의존성 주입)
NestJS에서의 IoC (프레임워크가 제어)
NestJS의 IoC 컨테이너가 하는 일:
@Injectable()데코레이터를 보고 클래스를 등록2. 의존성 주입 (Dependency Injection)
"의존성(Dependency)"이란?
의존성 = A가 B를 사용할 때, "A는 B에 의존한다"
Car는Engine을 사용함Engine없이는Car가 제대로 작동할 수 없음"주입(Injection)"이란?
주입 = 외부에서 안으로 넣어주는 것
"의존성 주입(Dependency Injection)" 전체 의미
"객체가 필요로 하는 것(의존성)을 외부에서 넣어주는(주입) 것"
왜 외부에서 주입받는 게 좋을까?
NestJS에서의 의존성 주입
정리:
핵심: "내가 필요한 것을 내가 만들지 않고, 외부에서 받아 쓴다!"
3. NestJS 아키텍처 구조
Controller (컨트롤러)
역할: HTTP 요청을 받고 응답을 반환하는 입구
특징:
@Get(),@Post(),@Put(),@Delete())@Body(),@Param(),@Query())Service (서비스)
역할: 실제 비즈니스 로직을 처리하는 곳
특징:
@Injectable()데코레이터 필수Repository (레포지토리)
역할: 데이터베이스와 직접 통신하는 계층
특징:
find,save,update,delete)Repository상속Entity (엔티티)
역할: 데이터베이스 테이블 구조를 정의하는 클래스
특징:
Module (모듈)
역할: 관련된 기능들을 하나로 묶는 컨테이너
특징:
imports: 다른 모듈 가져오기controllers: 컨트롤러 등록providers: Service, Repository 등록 (DI 컨테이너에 등록)exports: 다른 모듈에 제공비교 정리
왜 이렇게 나눌까?