μλ¬Έ: https://docs.nestjs.com/fundamentals/circular-dependency
Circular dependency
A circular dependency occurs when two classes depend on each other. For example, class A needs class B, and class B also needs class A. Circular dependencies can arise in Nest between modules and between providers.
μν μ’ μμ±μ λ ν΄λμ€κ° μλ‘ μ’ μλ λ λ°μνλ€. μλ₯Ό λ€μ΄, ν΄λμ€Aκ° ν΄λμ€ Bλ₯Ό νμλ‘νκ³ , λν ν΄λμ€Bκ° ν΄λμ€ Aλ₯Ό νμλ‘ νλ κ²½μ°μ΄λ€. μν μ’ μμ±μ λͺ¨λκ³Ό νλ‘λ°μ΄λ μ¬μ΄μμ μΌμ΄λ μ μλ€.
While circular dependencies should be avoided where possible, you can't always do so. In such cases, Nest enables resolving circular dependencies between providers in two ways. In this chapter, we describe using forward referencing as one technique, and using the ModuleRef class to retrieve a provider instance from the DI container as another.
μν μ’ μμ±μ κ°λ₯ν νΌν΄μΌνμ§λ§, νμ κ·Έλ΄μλ§μ μλ€. μ΄λ, Nestλ λκ°μ§ λ°©λ²μΌλ‘ νλ‘λ°μ΄λ μ¬μ΄μ μν μ’ μμ±μ ν΄κ²°ν μ μκ²νλ€. μ΄ μ±ν°μμ forward referencingμ μ¬μ©νλ λ°©λ²κ³Ό ModuleRefμ μ¬μ©ν΄ DI containerμμ νλ‘λ°μ΄λ μΈμ€ν΄μ€λ₯Ό κ²μνλ λ°©λ²μ μ€λͺ ν κ²μ΄λ€.
*retrieve: λμ°Ύμμ€λ€, νμνλ€, κ²μνλ€
We also describe resolving circular dependencies between modules.
λν λͺ¨λκ°μ μν μ’ μμ±μ ν΄κ²°νλ λ°©λ²μ μ€λͺ ν κ²μ΄λ€.
A circular dependency might also be caused when using "barrel files"/index.ts files to group imports. Barrel files should be omitted when it comes to module/provider classes. For example, barrel files should not be used when importing files within the same directory as the barrel file, i.e. cats/cats.controller should not import cats to import the cats/cats.service file. For more details please also see this github issue.
μν μ’ μμ±μ "barrel files"/index.tsμΌλ‘ κ·Έλ£Ή μν¬ν ν λμλ λ°μν μ μλ€. λͺ¨λ/νλ‘λ°μ΄λ ν΄λμ€λ€λ‘ κ°μ Έμμ§λ λ°°λ΄ νμΌλ€μ΄ λΉ λ¨λ €μ§ μ μλ€. μλ₯Ό λ€μ΄, λ°°λ΄ νμΌλ€μ΄ κ°μ λλ ν 리 νμΌμμμ μν¬νΈλ λ λ°°λ΄ νμΌλ€μ΄ μ¬μ©λμ§ μμ μ μλ€. cats/cats.controllerλ cats/cats.serviceνμΌμ μν¬νΈνκΈ° μν΄μ catsλ₯Ό μν¬νΈν΄μ μλλ€. λ μμΈν λ΄μ©μ κΉν μ΄μ μ°Έκ³ .
Forward reference
A forward reference allows Nest to reference classes which aren't yet defined using the forwardRef() utility function. For example, if CatsService and CommonService depend on each other, both sides of the relationship can use @Inject() and the forwardRef() utility to resolve the circular dependency. Otherwise Nest won't instantiate them because all of the essential metadata won't be available. Here's an example:
forward referenceμ Nestκ° μ νΈλ¦¬ν° ν¨μ forwardRef()μ μ¬μ©ν΄ μμ§ μ μλμ§ μμ ν΄λμ€λ€μ μ°Έμ‘°ν μ μκ²νλ€. μλ₯Ό λ€μ΄, λ§μ½ CatsServiceμ CommonServiceμ΄ μλ‘λ₯Ό μμ‘΄νλ€ νμλ, λ κ΄κ³λ @Inject()μ forwardRef() μ νΈλ¦¬ν°λ₯Ό μ¬μ©ν΄ μν μ’ μμ±μ ν΄κ²°ν μ μλ€. κ·Έλ μ§ μμΌλ©΄, νμ λ©νλ°μ΄ν°κ° μ ν¨νμ§ μκΈ° λλ¬Έμ, Nestλ λ μλΉμ€μ μΈμ€ν΄μ€λ₯Ό μμ±νμ§ μλλ€. νκ°μ§ μμλ₯Ό 보μ.
@Injectable()
export class CatsService {
constructor(
@Inject(forwardRef(() => CommonService))
private commonService: CommonService,
) {}
}
HINT The forwardRef() function is imported from the @nestjs/commonpackage.
ννΈ forwardRef() ν¨μλ @nestjs/common ν¨ν€μ§μμ μν¬νΈνλ€.
That covers one side of the relationship. Now let's do the same with CommonService:
μ΄κ²μ νμͺ½μ κ΄κ³λ₯Ό ν΄κ²°ν΄μ€λ€. μ΄μ , CommonServiceμλ κ°μ λ°©μμΌλ‘ 컀λ²ν΄λ³΄μ.
(*CommonService ↔ CatServiceμΈλ°, CatServiceμͺ½λ§ forwardRef()λ₯Ό μ¬μ©ν΄ 컀λ²νλ€λ λ»)
@Injectable()
export class CommonService {
constructor(
@Inject(forwardRef(() => CatsService))
private catsService: CatsService,
) {}
}
The order of instantiation is indeterminate. Make sure your code does not depend on which constructor is called first. Having circular dependencies depend on providers with Scope.REQUEST can lead to undefined dependencies. More information available here
μΈμ€ν΄μ€νμ μμλ μ½κ² κ°λ ν μ μλ€. μ½λκ° λ¨Όμ νΈμΆλλ μμ±μμ λ°λΌ λ¬λΌμ§μ§ μλμ§ νμΈν΄μΌνλ€. Scope.REQUEST μ νλ‘λ°μ΄λμ μν΄ μν μ’ μμ±μ κ°λκ²μ undefined dependenciesλ‘ μ΄λλ€. μμΈν μ 보λ μ¬κΈ°λ‘ (?)
ModuleRef class alternative
An alternative to using forwardRef() is to refactor your code and use the ModuleRef class to retrieve a provider on one side of the (otherwise) circular relationship. Learn more about the ModuleRef utility class here.
forwardRef()μ μ¬μ©νλ κ²μμ λμ ν΄, μ½λλ₯Ό 리νν°λ§νκ±°λ ModuleRefν΄λμ€λ₯Ό μ¬μ©ν΄ μν κ΄κ³μ νμͺ½(λ€λ₯Έμͺ½) νλ‘λ°μ΄λλ₯Ό κ²μν μ μλ€.
Module forward reference
In order to resolve circular dependencies between modules, use the same forwardRef() utility function on both sides of the modules association. For example:
λ λͺ¨λκ°μ μν μ’ μμ±μ ν΄κ²°νκΈ° μν΄, κ°μ forwardRef()μ νΈλ¦¬ν° ν¨μλ₯Ό μμͺ½ λͺ¨λ μ°κ΄μ±μ μ¬μ©νλ€. μμ:
@Module({
imports: [forwardRef(() => CatsModule)],
})
export class CommonModule {}
'πμ½λ© κ³΅λΆ > μ½λ©κ³΅λΆ μ΄λͺ¨μ λͺ¨' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
server) νΈλμ΄λ μ£½μ΄μ κ°μ£½μ λ¨κΈ°κ³ , κ°λ°μλ μ£½μ΄μ λ‘κ·Έλ₯Ό λ¨κΈ΄λ€. (0) | 2022.03.01 |
---|---|
VScode ) Beautify μ€μ (0) | 2022.01.15 |
Api λ¬Έμ μλνμ λν κ°μΈμ μΈ λ Έλ ₯κ³Ό νκΈ°... (0) | 2021.12.26 |
NCP) nCloud λ€μ΄λ² API Signature Key μμ± (0) | 2021.10.09 |
Server ) RESTful API, μμ£Ό μ¬μ©νλ Status code μ 리 λ° μμ (0) | 2021.10.04 |