A minimal dependency injection container, loosely based on Angular's DI.
npm install --save mindi
import { Inject, Injector } from 'mindi';
import { SomeDependency } from './SomeDependency';
export class MyClass {
constructor(@Inject(SomeDependency) public someDependency: SomeDependency) {}
}
const injector = new Injector([ SomeDependency, MyClass ]);
const myClass = injector.get(MyClass);
myClass.someDepenedency instanceof SomeDependency; // => true
Injectors are heirarchical. So an injector can have a parent.
class MyClass {}
const injector = new Injector([ MyClass ]);
const childInjector = new Injector([], injector);
childInjector.get(MyClass) === injector.get(MyClass); // => true
You can inject dependencies two ways.
You inject dependencies directly into the constructor.
import { Token, Injector, Inject } from 'mindi';
const token = new Token('someService');
class MyClass {
constructor(@Inject(token) public service: string) {}
}
const injector = new Injector([ { provide: token, useValue: 'blorg' } ]);
const myClass = injector.get(MyClass);
myClass.service; // => 'blorg'
You can inject dependencies by having them assigned to properties on the instance.
import { Token, Injector, Inject } from 'mindi';
const token = new Token('someService');
class MyClass {
@Inject(token) service: string;
}
const injector = new Injector([ { provide: token, useValue: 'blorg' } ]);
const myClass = injector.get(MyClass);
myClass.service; // => 'blorg'
Note, the injected properties will NOT be available in the constructor, but will be availble in the PostConstruct
annotated hooks.