nest icon indicating copy to clipboard operation
nest copied to clipboard

feat(core): Add multi provider option

Open BrunnerLivio opened this issue 5 years ago • 7 comments

PR Checklist

Please check if your PR fulfills the following requirements:

  • [x] The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • [ ] Tests for the changes have been added (for bug fixes / features)
  • [ ] Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

No multi provider option

Issue Number: #770 #1517

What is the new behavior?

Support the multi option for providers


const MULTI_TOKEN = Symbol('MY_MULTI_TOKEN');

class Test {
  value = 'B';
}

@Module({
  providers: [
    {
      multi: true,
      useValue: 'A',
      provide: MULTI_TOKEN
    },
  ]
})
class AModule { }

@Module({
  providers: [
    {
      multi: true,
      useClass: Test,
      provide: MULTI_TOKEN
    },
  ]
})
class BModule { }

@Module({
  imports: [AModule, BModule],
  providers: [
    {
      multi: true,
      useFactory: () => 'Application',
      provide: MULTI_TOKEN,
    },
  ]
})
class ApplicationModule { }

async function bootstrap() {
  const app = await NestFactory.createApplicationContext(ApplicationModule);

  const result = app.get(MULTI_TOKEN);
  console.log(result);
}

bootstrap();

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

BrunnerLivio avatar Jun 26 '19 21:06 BrunnerLivio

Pull Request Test Coverage Report for Build 3416

  • 40 of 53 (75.47%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.3%) to 94.21%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/injector/module.ts 38 51 74.51%
<!-- Total: 40 53
Totals Coverage Status
Change from base Build 3413: -0.3%
Covered Lines: 3531
Relevant Lines: 3748

💛 - Coveralls

coveralls avatar Jun 27 '19 15:06 coveralls

@kamilmysliwiec Add 6.8.0 or 7.0 ?

zuohuadong avatar Sep 26 '19 01:09 zuohuadong

@kamilmysliwiec will this get implemented at some point, or should it just be scratched completely? :smile:

marcus-sa avatar Jul 23 '20 10:07 marcus-sa

I'm more inclined towards this syntax https://github.com/nestjs/nest/issues/4786 (composite providers) instead (more explicit)

kamilmysliwiec avatar Jul 23 '20 11:07 kamilmysliwiec

So you ask people to see #4786 in this PR, and closed #4786 to ask people to see this PR? If I understand correctly, you still prefer this implementation, right?

Char2sGu avatar Mar 29 '22 09:03 Char2sGu

Will it be added? I really need something like this

emiliosanches avatar Mar 31 '22 14:03 emiliosanches

News?

https://github.com/inversify/InversifyJS/blob/master/wiki/multi_injection.md

WilliamPeralta avatar Jun 20 '22 09:06 WilliamPeralta

I keep encountering scenarios where multi providers would be very useful... 😢

jonapgartwohat avatar Jan 18 '23 21:01 jonapgartwohat

https://github.com/nestjs/nest/issues/770#issuecomment-1412029060

kamilmysliwiec avatar Feb 01 '23 13:02 kamilmysliwiec