Nest: Cross modules dependency injection

Created on 20 Oct 2017  路  9Comments  路  Source: nestjs/nest

Suppose I have
Module A

  • Controller A
  • Component A
    Module B
  • Controller B
  • Component B

If I want to use controller B in controller A, is dependency injection a good choice?
But in order to use dependency injection, I should do following in Module A:
controllers: [Controller A],
components: [Component A, Controller B, Component B]

Is it a good practice that references other module's controller in Module A for cross-module usage?

Thx.

Most helpful comment

hey @kwtsangaf
if you want to use a component B in the controller A, you should exports the component B from the module B and then import the module B in the module A to use the component B in the controller A

// Module B
@Module({
    controllers: [ControllerB],
    components: [ComponentB],
    exports: [componentB]
})

// Module A
@Module({
    modules: [ModuleB],
    controllers: [ControllerA],
    components: [ComponentA]
})

// Controller A of the Module A
@Controller()
export class controllerA {
    constructor(private readonly componentB: ComponentB) { }
}

All 9 comments

@kwtsangaf I suppose you mixed things slightly - what do you mean when you say

want to use controller B in controller A

Did you mean use component B in controller A?

hey @kwtsangaf
if you want to use a component B in the controller A, you should exports the component B from the module B and then import the module B in the module A to use the component B in the controller A

// Module B
@Module({
    controllers: [ControllerB],
    components: [ComponentB],
    exports: [componentB]
})

// Module A
@Module({
    modules: [ModuleB],
    controllers: [ControllerA],
    components: [ComponentA]
})

// Controller A of the Module A
@Controller()
export class controllerA {
    constructor(private readonly componentB: ComponentB) { }
}

The controllers are responsible of the requests, so if you need Controller A inside controller B, perhaps you have so much application logic inside controller A. You should extract that logic to a component and inject that componen in controller A and controller B

I think there is a miss understanding between controller and component in the question. maybe ^^

Am I the only one who thinks that having hierarchical injector is unnecessary and just makes things complex? I mean for Angular is ok and makes sense (lazy loading, services per component and etc.) but here having just one global injector would be perfectly fine. Ex. the Spring framework has one global injector. Why making the things complex when they can be simple (KISS).
Using the class references as tokens and symbols for custom tokens solve the issue of using string tokens and accidentally overriding components in the injector.

Hi @kwtsangaf,
It's not possible to inject controller by another controller. Let's move your logic into the components.
@saskodh hierarchical injector exists to follow the idea of the domain encapsulation. Each module is some kind of facade and decides which component/service/anything might be used outside.

Thank you for all reply. Yup I miss understanding between controller and component. I thought I cannot call component directly but need to pass through controller instead. I am wrong.

For the structure design, basically,
component B only used in module A by now, it should be under module A.
But I want it able to be used by other modules in future
so I isolate component B into module B.
I don't know whether this approach is good or not.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

artaommahe picture artaommahe  路  3Comments

cojack picture cojack  路  3Comments

hackboy picture hackboy  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments

janckerchen picture janckerchen  路  3Comments