Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchical injector #59

Closed
AurelieV opened this issue May 12, 2017 · 7 comments
Closed

Hierarchical injector #59

AurelieV opened this issue May 12, 2017 · 7 comments
Assignees

Comments

@AurelieV
Copy link

AurelieV commented May 12, 2017

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.

@cojack
Copy link
Contributor

cojack commented May 12, 2017

@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

@AurelieV
Copy link
Author

AurelieV commented May 12, 2017

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

@AurelieV
Copy link
Author

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.

@kamilmysliwiec
Copy link
Member

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.

@AurelieV
Copy link
Author

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

@kamilmysliwiec
Copy link
Member

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.

@lock
Copy link

lock bot commented Sep 25, 2019

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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants