nest icon indicating copy to clipboard operation
nest copied to clipboard

Support multi option for providers

Open cgatian opened this issue 6 years ago • 6 comments

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 avatar Jun 07 '18 20:06 cgatian

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

cgatian avatar Jun 08 '18 00:06 cgatian

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

OJezu avatar Nov 26 '19 14:11 OJezu

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

fyfey avatar Dec 12 '19 13:12 fyfey

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 {}

flisboac avatar Feb 11 '20 23:02 flisboac

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.

jrista avatar Apr 30 '21 02:04 jrista

@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.

kamilmysliwiec avatar Apr 30 '21 07:04 kamilmysliwiec

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 https://github.com/nestjs/nest/pull/2460#discussion_r298914246

kamilmysliwiec avatar Feb 01 '23 13:02 kamilmysliwiec