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

Dependency injection without ts metadata #1080

Merged
merged 4 commits into from
May 18, 2022
Merged

Dependency injection without ts metadata #1080

merged 4 commits into from
May 18, 2022

Conversation

tobiasdiez
Copy link
Member

@tobiasdiez tobiasdiez commented May 17, 2022

Esbuild doesn't support 'emitDecoratorMetadata' and doesn't intend to support it in the future: evanw/esbuild#257, evanw/esbuild#509. There is also no real hope that one could write an esbuild plugin that does the job in the near future: evanw/esbuild#111. Our current solution to run typescript on every module is slow and kind of defeats the purpose of esbuild.

Thus, we migrate tsyringe nice concept to automatically recognize the type of the dependency (using the emitted metadata) to explicitly annotating each dependency with @inject('Type'). This is similar to https://github.com/inversify/InversifyJS/blob/master/wiki/classes_as_id.md.

Things that one could still improve in the future:

  • Allow types to be passed in instead of strings, e.g. @inject(AuthService)
  • Extend the injectSymbol method to automatically register a symbol -> class alias so that we don't have to do this manually in tsyringe.config (there might be problems with circular imports if this approach is used).
  • Remove reflect-metadata as soon as Usage without Reflect microsoft/tsyringe#180 is fixed

@tobiasdiez tobiasdiez marked this pull request as ready for review May 18, 2022 11:54
@tobiasdiez tobiasdiez merged commit b98ab94 into main May 18, 2022
@tobiasdiez tobiasdiez deleted the depinjection branch May 18, 2022 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant