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

Clean up NestJS providers for dependency injection #1560

Open
sbliven opened this issue Dec 12, 2024 · 1 comment
Open

Clean up NestJS providers for dependency injection #1560

sbliven opened this issue Dec 12, 2024 · 1 comment

Comments

@sbliven
Copy link
Contributor

sbliven commented Dec 12, 2024

Summary

NestJS expects to manage the instantiation of providers (eg any classes marked @Injectable() as well as some less common provider definitions). Instead they should be provided by a single module. That way NestJS creates a singleton instance available across the app.

SciCat should remove cases where providers are instantiated and replace these with dependency injection (DI).

Steps to Reproduce

The effect can be seen by putting a logging statement in a provider constructor. For instance, the CaslAbilityFactory is instantiated 13 times; configure() gets called several places directly rather than via ConfigService, AuthService is provided twice, etc.

Current Behaviour

Multiple instances are harmless as long as the provider doesn't have dependencies or side effects. However it might decrease performance and it doesn't follow best practices

Expected Behaviour

  • Providers are provided in a single module and exported
  • Other providers that need access to the singleton should do so via dependency injection
  • Tests may need to construct a TestModule to ensure DI gets called.

Details

#1539 removes many of the CaslAbilityFactory instances and includes some examples of how to construct the TestModules. In many cases the entire module can just be mocked, avoiding dependency injection.

@sbliven sbliven changed the title Don't instantiate providers Don't instantiate NestJS providers directly Dec 12, 2024
@sbliven sbliven changed the title Don't instantiate NestJS providers directly Clean up NestJS providers for dependency injection Dec 12, 2024
@sbliven
Copy link
Contributor Author

sbliven commented Dec 12, 2024

I propose putting this on hold until after #1539 (and the rest of release-jobs) gets merged into main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant