Nest: Hierarchical injector

Created on 12 May 2017  路  7Comments  路  Source: nestjs/nest

Hi,
in order to make more reusable code, I would like to create a module which use a component which is not a part of this module, nor include in an imported module. The component will be inject by the ApplicationModule (main module). It seems that this is not supported?

Example:
AuthenticationModule has different controllers which use a UserService dependency.
UserModule also has different controllers which use a UserService dependency.

Depending on application, the UserService may be implemented in different way. What I want is defining this in the ApplicationModule for all modules, once for all.

@Module({
    modules: [ AuthenticationModule, UserModule ]
    components: [
        { provide: UserService, useClass: MyCustomUserService }
    ]
})
class ApplicationModule {}

@Module({
    controllers: [],
    components: [ AuthenticationService ],
    exports: [ 
        AuthenticationService
    ]
})
class AuthenticationModule {}

export class AuthenticationService {
    constructor(private userService: UserService) {}
}

I was expecting below code to work (similar to angular), but the userService dependency is not found.

type

Most helpful comment

Hi @AurelieV,
I made a set of improvements for a dependency injector, so now your example should works fine. Please, update your packages (since ~2.* version @nestjs/core, @nestjs/common etc.) into latest versions.

All 7 comments

@AurelieV try:

@Module({
    modules: [ UserModule ], 
    controllers: [],
    components: [ AuthenticationService ],
    exports: [ 
        AuthenticationService
    ]
})
class AuthenticationModule {}

as it's described here: https://kamilmysliwiec.gitbooks.io/nest/content/advanced/shared-module.html

The problem is I do not want my two modules being dependant on each others.
AuthenticationService can be use without UserModule.
UserService is not part of UserModule

An other example will be to inject Configuration class, and re-use your modules accross different applications. You inject the app-specific configuration on app module decorator, you import the different modules and everything will be working for this app.

Hi @AurelieV,
The problem is that modules in Nest are a singletons, so it is possible to share same instances between them using some kind of shared module. Hierarchical injection in this case makes impossible to share components across e.g. two modules. What you show is definitely needed and I'll make it supported as soon as possible, but it could reflect on shared modules mechanism. It seems we need another feature, which allows to mark modules as shared / singletons. Scope also should be possible to set.

Yep.
Will also be greet to import module into other with only components, and not controllers (equivalent to forRoot and forChild in angular)

Hi @AurelieV,
I made a set of improvements for a dependency injector, so now your example should works fine. Please, update your packages (since ~2.* version @nestjs/core, @nestjs/common etc.) into latest versions.

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

tronginc picture tronginc  路  3Comments

hackboy picture hackboy  路  3Comments

janckerchen picture janckerchen  路  3Comments

mishelashala picture mishelashala  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments