Why use `deps` property in DI
Asked Answered
B

1

13

Here is the code snippet from angular.io:

{ provide: RUNNERS_UP,    useFactory:  runnersUpFactory(2), deps: [Hero, HeroService] }

...

export function runnersUpFactory(take: number) {
  return (winner: Hero, heroService: HeroService): string => {
    /* ... */
  };
};

My question is why deps property is used here? What are the general cases for using deps?

Billi answered 24/1, 2017 at 6:59 Comment(4)
See the manual angular.io/docs/ts/latest/api/core/index/… , it is quite exhaustive on this.Faso
@estus, thanks, I've seen the manual. I usually ask a question to get a broader picture, while the manual just says that it's used with factory providers. But is it limited to it? Are there any other use cases? etc. These are the questions I have in my headBilli
Yes, it is limited to factories. The benefit of TS is that properties don't come out of nowhere, they are defined with interfaces (FactoryProvider here).Faso
@estus, got itl, thanksBilli
D
15

This is a way to tell Angular dependency injections what dependencies it needs to inject to the factory function returned by runnersUpFactory.

For services there is the @Injectable() class to tell DI that it needs to analyze the constructor parameter of this class (same for @Component(), @Directive(), and @Pipe()), but this seems not to work for functions. Therefore they introduced the deps parameter.

DI will look up a provider using the key Hero and another one using HeroService and then will pass them as parameters to the factory function in the same order.

https://angular.io/docs/ts/latest/api/core/index/FactoryProvider-interface.html

deps : any[] A list of tokens which need to be resolved by the injector. The list of values is than used as arguments to the useFactory function.

Dismay answered 24/1, 2017 at 7:2 Comment(11)
thanks, so it's basically only relevant when useFactory is used, correct? when the other providers are used, like useClass, useValue and useExisting, it has no purpose?Billi
Exactly - only for useFactoryAlvord
Great, thanks. Just a quick follow-up, is there any other provider besides the ones I mentioned in my previous comment?Billi
That's the kind of inconsistency I don't like on Angular 2Goodhen
github.com/angular/angular/blob/…Alvord
@Goodhen A function cannot be annotated for DI with decorators, that's TS limitation. If you're after consistency, you can always annotate it like factoryFn.parameters = [[new Inject(dep)]] instead of deps: [dep], that's common syntax for all DI.Faso
@Estus, Gunther! Thanks to both for very useful insights ;-)Goodhen
@GünterZöchbauer Are you sure they're only for useFactory ? I've asksed it yesterday and was told - no.Kajdan
@RoyiNamir seems reasonable. Might be handy in rare (I guess) situations. Haven't seen it before.Alvord
Its not documented well, but deps can be used with useClass: #48595444Chilli
@pixelbits thanks for the info. Royi linked to your answer already. It makes sense, it just didn't occur to me it could be useful, but I can imagine scenarios.Alvord

© 2022 - 2024 — McMap. All rights reserved.