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

Autowire dependencies without annotations #13556

Closed
1 task done
davidroberto opened this issue May 10, 2024 · 2 comments
Closed
1 task done

Autowire dependencies without annotations #13556

davidroberto opened this issue May 10, 2024 · 2 comments
Labels
needs triage This issue has not been looked into type: enhancement 🐺

Comments

@davidroberto
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

When implementing clean / hexagonal architecture and the dependency inversion, you want to type an interface in your services constructor instead of the implementation class.
For exemple, if you need to persist data, you'll type a repository interface instead of the type orm / prisma orm or whatever you want to use. In order to keep the application or domain layers clean of infrastructure references, you don't want to use the Inject annotation to inject the dependency, but instead you use the module definition to inject the dependency in your services (using useFactory for exemple).
But it requires some boilerplate code, especially if you have a lot of modules / services.

Describe the solution you'd like

In frameworks like symfony, the type hinted dependency in the service is automatically resolved, even if you type an interface. The dependency injection component find the class that implement the interface and inject it.
And if multiple classes implement the same typed interface, you can specify which class you want to inject, so the the "autowire" feature is just a default if you have just one class implementing the typed interface, but it save a lot of time / boilerplate code.
This autowire feature could also be turned off, or even disabled by default, so that it doesn't change the default behavior of the DI.

Could this "autowire" feature be a good idea in Nest? If so, i can look into it (with some help) :)

Teachability, documentation, adoption, migration strategy

This autowires could be disabled by default, so that it doesn't change the default behavior of the DI. And if you want to enabled it, the the bootstrap function of the main.ts file could be the place to do it.

What is the motivation / use case for changing the behavior?

Save boilerplate code for the DI / dependency inversion

@davidroberto davidroberto added needs triage This issue has not been looked into type: enhancement 🐺 labels May 10, 2024
@micalevisk
Copy link
Member

There is a community package for this already. It works by enforcing convention over configuration, which I don't think that will be good for NestJS because it won't be flexible enough

@kamilmysliwiec
Copy link
Member

Thanks for your suggestion!

This has been discussed in the past and we decided to not implement it in the foreseeable future.

If you think your request could live outside Nest's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

@nestjs nestjs locked and limited conversation to collaborators May 11, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into type: enhancement 🐺
Projects
None yet
Development

No branches or pull requests

3 participants