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

When do you inject vs. just import? (config) #74

Open
danielbrauer opened this issue Jun 28, 2020 · 3 comments
Open

When do you inject vs. just import? (config) #74

danielbrauer opened this issue Jun 28, 2020 · 3 comments

Comments

@danielbrauer
Copy link

My guess is that injecting dependencies is more important when they own resources, but I'm still curious why something like config isn't injected.

@salmanahmad94
Copy link

salmanahmad94 commented Jul 8, 2020

My guess is that the configuration is supposed to be part of the runtime environment and not a dependency. For example, if all your configuration is being imported by dotenv and loaded in the src/config/app.js file, it'd be better to add src/config as a global module resolved by babel. And then in individual services, you could import config like import config from 'config';

My guess is that injecting dependencies is more important when they own resources, but I'm still curious why something like config isn't injected.

@silesky
Copy link

silesky commented Dec 17, 2020

I don’t know if there are any hard and fast rules about this. It is a good question of “where do you draw the line”?

I think — since we are talking about global configuration of services that themselves are injected, its idiomatic for config to be an imported module.

Because node has a proper module system and imports can be stubbed about as easily as DI services, there isn’t any reason to use DI other than you prefer the semantics.

@elemanhillary
Copy link

You inject when Dependency is registered in the container and its not decorated with @service, and you imported when dependency is not registered but its decorated with @service

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

No branches or pull requests

4 participants