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

Support multi option for providers #770

Closed
cgatian opened this issue Jun 7, 2018 · 7 comments
Closed

Support multi option for providers #770

cgatian opened this issue Jun 7, 2018 · 7 comments
Labels
effort2: days scope: core state: core-team Someone from the NestJS core team is working on this issue or submitted this PR type: enhancement 🐺

Comments

@cgatian
Copy link

cgatian commented Jun 7, 2018

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ X] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Unable to pass multiple objects on one token.

Expected behavior

Providers should support he multi boolean that allows you to inject multiple providers.

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

Behavior similar to Angular

@cgatian
Copy link
Author

cgatian commented Jun 8, 2018

One use case I have is being able to set my typeDefs through a common injection token. Then during composition of the graphql layer I can compose all the typeDefs in use. Currently, these need to be manually imported. This would aid in modularizing a graphql schema

@marcus-sa marcus-sa mentioned this issue Sep 27, 2018
3 tasks
BrunnerLivio added a commit to BrunnerLivio/nest that referenced this issue Jul 1, 2019
BrunnerLivio added a commit to BrunnerLivio/nest that referenced this issue Aug 27, 2019
BrunnerLivio added a commit to BrunnerLivio/nest that referenced this issue Aug 27, 2019
BrunnerLivio added a commit to BrunnerLivio/nest that referenced this issue Aug 27, 2019
@OJezu
Copy link

OJezu commented Nov 26, 2019

Could this support priorities, for ordering imports? It's not in Angular, but similar feature can be found in Symfony DI container.

@Module({
  providers: [
    {
      provide: 'TEST',
      useClass: Test1,
      multi: true,
      priority: 0, // default
    },
    {
      provide: 'TEST',
      useClass: Test2,
      multi: true,
      priority: 1,
    }],
})

So now, @Inject('TEST') injects an array of [Test2, Test1].

Symfony documentation:
https://symfony.com/doc/current/service_container/tags.html#reference-tagged-services

@fyfey
Copy link

fyfey commented Dec 12, 2019

Please include this! It looks like a lot of the work has already be done? @BrunnerLivio

@flisboac
Copy link

Just a suggestion: it would be nice to be able to pass an abstract class as a token. It'd be one less language element to deal with when using an abstract class as a mere interface. For example:

abstract class Flavour {
  // The abstract interface here
}

class VanillaFlavour implements Flavour { /* ... */ }
class StrawberryFlavour implements Flavour { /* ... */ }

// ...

@Module({
  providers: [
    {
      provide: Flavour,
      useClass: VanillaFlavour,
      multi: true,
    },
    {
      provide: Flavour,
      useClass: StrawberryFlavour,
      multi: true,
    }],
})
export class FlavourModule {}

Compare with:

const FLAVOUR = Symbol('Flavour');

interface Flavour {
  // The abstract interface here
}

class VanillaFlavour implements Flavour { /* ... */ }
class StrawberryFlavour implements Flavour { /* ... */ }

// ...

@Module({
  providers: [
    {
      provide: FLAVOUR,
      useClass: VanillaFlavour,
      multi: true,
    },
    {
      provide: FLAVOUR,
      useClass: StrawberryFlavour,
      multi: true,
    }],
})
export class FlavourModule {}

@kamilmysliwiec kamilmysliwiec added state: core-team Someone from the NestJS core team is working on this issue or submitted this PR effort2: days and removed type: todo 💚 labels Feb 2, 2021
@jrista
Copy link

jrista commented Apr 30, 2021

It looked like this was changed from todo to core-team in February. Is this being done? I've run into several use cases since I first found this around the beginning of the year where I could have used multi providers. Its a pretty useful and important feature...and would really love to see it in NestJS.

@kamilmysliwiec
Copy link
Member

@jrista this is not a top priority as you can already define providers composed of other providers explicitly (using factory providers). We have a PR in place for this feature as well, there are just several minor things to get done & make sure they work. Let me lock this issue for the time being.

@nestjs nestjs locked and limited conversation to collaborators Apr 30, 2021
@kamilmysliwiec
Copy link
Member

Just to let everyone know - this won't be implemented as Nest already lets you register composed providers explicitly (non-implicit syntax won't be added). For example:

{
  provide: 'MY_COMPOSED_PROVIDER',
  useFactory: (a, b, c) => [a, b, c],
  inject: [A, B, C] // where A, B, C are providers that implements a common interface
}

For more information on why the implicit syntax wouldn't be easy to implement & maintain (compared to frameworks like Angular), check out this convo #2460 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
effort2: days scope: core state: core-team Someone from the NestJS core team is working on this issue or submitted this PR type: enhancement 🐺
Projects
None yet
Development

No branches or pull requests

6 participants