Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maxleiko/ts-injector
TypeScript Dependency Injector using reflect-metadata and tsc 1.5+ with es7 experimental features enabled
https://github.com/maxleiko/ts-injector
Last synced: 7 days ago
JSON representation
TypeScript Dependency Injector using reflect-metadata and tsc 1.5+ with es7 experimental features enabled
- Host: GitHub
- URL: https://github.com/maxleiko/ts-injector
- Owner: maxleiko
- License: mit
- Created: 2015-08-07T13:30:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-02-26T17:28:56.000Z (over 8 years ago)
- Last Synced: 2024-10-30T14:55:00.522Z (18 days ago)
- Language: TypeScript
- Size: 10.7 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# injector
TypeScript Dependency Injection using `reflect-metadata` and **typescript** 1.7+ with es7 experimental features enabled### How it works
The purpose of this module is to allow you to do something like that:```ts
// MyClass.ts
import { Inject } from 'ts-injector';export class MyClass {
@Inject('Foo')
private foo: Foo;doFoo(): void {
this.foo.foo();
}
}// Foo.ts
declare interface Foo {
foo(): void;
}
```And then create the instance of **MyClass** and inject the instance of **Foo** at runtime:
```ts
// test.ts
import { Injector } from 'ts-injector';
import { MyClass } from './MyClass';
import { Foo } from './Foo';// create the real Foo class
class FooImpl implements Foo {
foo(): void {
console.log('foo bar');
}
}// instantiate the injector
var injector = new Injector();
// register an instance of FooImpl to be bind with people asking for @Inject("Foo")
injector.register('Foo', new FooImpl());// create an instance of MyClass
var c = new MyClass();
// inject the needed dependencies
injector.inject(c);// now you can call
c.doFoo();
// prints "foo bar"
```### Dev env
Init the project:
```sh
npm install
```
Compile TypeScript:
```sh
npm run build
```
Run the tests
```sh
npm test
```