tsyringe
tsyringe copied to clipboard
New decorator injectableAll
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!
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
.
@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[]
@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.