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

Unable to inject dependency on Application #54

Closed
2 of 4 tasks
AntonyZ89 opened this issue Jul 25, 2023 · 3 comments
Closed
2 of 4 tasks

Unable to inject dependency on Application #54

AntonyZ89 opened this issue Jul 25, 2023 · 3 comments
Assignees
Labels
analysis In analysis for development

Comments

@AntonyZ89
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

Created a provider called "EventManager" and tried inject it on Application class to be invoked on postServerInitialization()

2023-07-25_14-50

But received this error on start the server:

2023-07-25_14-50

Steps to reproduce

  1. npm ci
  2. npm run dev

Expected behavior

2023-07-25_14-50

Package version

1.5.1

Node.js version

18.14.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

di-issue.zip

@AntonyZ89 AntonyZ89 added the needs triage Needs analysis label Jul 25, 2023
@AntonyZ89
Copy link
Contributor Author

After read the doc of Inversify I solved this problem doing the following changes:

application.provider.ts

const appInstance = new App();

export { appInstance as App };

to

export { App };

--

src/main.ts

const app = App.create(container);

to

const app = container.get(App);

app.create(container);

And it's works as expected:
2023-07-25_14-50

Opened this issue because I believe it is a problem in the template as it does not accept injecting dependency into the Application class.

@rsaz rsaz self-assigned this Aug 14, 2023
@rsaz rsaz added analysis In analysis for development and removed needs triage Needs analysis labels Aug 14, 2023
@rsaz
Copy link
Member

rsaz commented Aug 14, 2023

Hi @AntonyZ89 to resolve this issue in the proper manner I would recommend you to do this instead:

@provide(TwitchProvider)
class TwitchProvider {
    async getAuthProvider(): Promise<string> {
        return randomUUID();
    }
}

export { TwitchProvider };

Use the container to request services, instances like the code below:

@provide(App)
class App extends Application {
    private twitchProvider: TwitchProvider;

    constructor() {
        super();
        this.twitchProvider = container.get<TwitchProvider>(TwitchProvider);
    }

    protected configureServices(): void {
        Environments.checkAll();
    }

    // eslint-disable-next-line @typescript-eslint/no-empty-function
    protected postServerInitialization(): void {
        console.log(this.twitchProvider.getAuthProvider());
    }

    protected serverShutdown(): void {
        log(LogLevel.Info, "Server is shutting down", "logger-provider");
        super.serverShutdown();
    }
}

const appInstance = new App();

export { appInstance as App };

This is a very similar approach to .NET core on requesting services from the DI system, and how we are using at the moment on ExpressoTS

serviceProvider.GetRequiredService<SERVICES.Core.IAuthService>();

Let me know if it is working fine for you, so that I can close this ticket.

@AntonyZ89
Copy link
Contributor Author

@rsaz Thanks, it works!

@rsaz rsaz closed this as completed Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analysis In analysis for development
Projects
Status: Done
Development

No branches or pull requests

2 participants