Using the inversify
library we get dependency injection capabilities.
For a class to be injectable add the @injectable()
decorator to it and register it in the ioc.config.ts
and ioc.types.ts
files
// myNeed.ts
import { injectable } from 'inversify';
@injectable()
class MyNeed { }
// ioc.types.ts
const TYPES = {
MyNeed: Sumbol("MyNeed"),
};
// ioc.config.ts
import { MyNeed } from './myNeed';
...
container.bind<MyNeed>(TYPES.MyNeed).to(MyNeed);
When in need of an injectable entity use the @inject(<TYPE>)
decorator in the constructor as below.
import { inject } from 'inversify';
import { TYPES } from './ioc.types';
class InNeed {
constructor(@inject(TYPES.MyNeed) myNeed: MyNeed) { }
}