diff --git a/README.md b/README.md index 0b46ad3..9fca48b 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ constructor injection. - [inject()](#inject) - [injectAll()](#injectall) - [scoped()](#scoped) + - [injectableAll()](#injectableall) - [Container](#container) - [Injection Token](#injection-token) - [Providers](#providers) @@ -219,6 +220,25 @@ Class decorator factory that registers the class as a scoped dependency within t class Foo {} ``` +### injectableAll() + + Class decorator factory that registers the classes with the same token within + the global container +#### Usage + +```typescript +@injectableAll('tags') +class Foo {} + +@injectableAll('tags') +class Bar {} + +@injectable() +class FooBar { + constructor(@injectAll('tags') private fooBars: any[]) {} +} +``` + ## Container The general principle behind [Inversion of Control](https://en.wikipedia.org/wiki/Inversion_of_control) (IoC) containers diff --git a/src/decorators/index.ts b/src/decorators/index.ts index e6caae3..13ce3ce 100644 --- a/src/decorators/index.ts +++ b/src/decorators/index.ts @@ -5,3 +5,5 @@ export {default as registry} from "./registry"; export {default as singleton} from "./singleton"; export {default as injectAll} from "./inject-all"; export {default as scoped} from "./scoped"; +export {default as injectableAll} from "./injectable-all"; + diff --git a/src/decorators/injectable-all.ts b/src/decorators/injectable-all.ts new file mode 100644 index 0000000..d9921e0 --- /dev/null +++ b/src/decorators/injectable-all.ts @@ -0,0 +1,19 @@ +import constructor from "../types/constructor"; +import {instance as globalContainer} from "../dependency-container"; +import injectable from "./injectable"; +import InjectionToken from "../providers/injection-token"; + +/** + * Class decorator factory that registers the classes with the same token within + * the global container. + * + * @return {Function} The class decorator + */ +function injectableAll(token: InjectionToken): (target: constructor) => void { + return function(target: constructor): void { + injectable()(target); + globalContainer.register(token, target) + }; +} + +export default injectableAll;