tsyringe icon indicating copy to clipboard operation
tsyringe copied to clipboard

New decorator injectableAll

Open axotion opened this issue 4 years ago • 4 comments

Description

Howdy!

I want to open discussion about PoC of injectableAll decorator. I guess it's still missing feature when we have injectAll and don't have any decorator to aggregate injectables under one token.

@injectableAll('tags')
class Foo {}
@injectableAll('tags')
class Bar {}
@injectable()
class FooBar {
    constructor(@injectAll('tags') private fooBars: any[]) {}
}

(eg. refer PR https://github.com/microsoft/tsyringe/pull/40 )

I created working implementation in this PR, but it's still more PoC than actuall implementation for missing decorator, so let's discuss!

axotion avatar Jul 11 '20 17:07 axotion

CLA assistant check
All CLA requirements met.

ghost avatar Jul 11 '20 17:07 ghost

Not part of the project so just my comments. Looks useful, but I think the naming is a bit confusing. Maybe injectableTagged or injectableAs. Alternatively, maybe it's simpler to simply allow the parameter on injectable.

ryall avatar Jul 21 '20 08:07 ryall

@injectable()
@registry([{token: 'Bar', useClass: Foo}])
class Foo implements Bar {}
@injectable()
@registry([{token: 'Bar', useClass: Baz}])
class Baz implements Bar {}

and

@registry([
     // registry is optional, all you need is to use the same token when registering
     {token: "Bar", useToken: Foo}, // can be any provider
     {token: "Bar", useToken: Baz}
 ])
 class MyRegistry {}
 const myBars = container.resolveAll<Bar>("Bar"); // myBars type is Bar[]

visualkhh avatar Oct 06 '22 22:10 visualkhh

@injectable()
@registry([{token: 'Bar', useClass: Foo}])
class Foo implements Bar {}
@injectable()
@registry([{token: 'Bar', useClass: Baz}])
class Baz implements Bar {}

and

@registry([
     // registry is optional, all you need is to use the same token when registering
     {token: "Bar", useToken: Foo}, // can be any provider
     {token: "Bar", useToken: Baz}
 ])
 class MyRegistry {}
 const myBars = container.resolveAll<Bar>("Bar"); // myBars type is Bar[]

I wouldn't call this a workaround to the proposed PR. Hard coding each token to a symbol in a different location than where the tokens are defined isn't very helpful. Maybe I am misunderstanding but writing a class to bind the tokens isn't as helpful as what is proposed in this PR where we can do this spread out through the codebase.

seriouslag avatar Mar 19 '23 01:03 seriouslag