Power DI
A lightweight Dependency Injection library. Using es6 and other features, remove unnecessary concepts, easy and convenient to use.
Install
npm i power-di --save
Example
a simple example
import { IocContext } from 'power-di';
class AService { }
const context = IocContext.DefaultInstance;
context.register(AService);
const aService = context.get(AService);
inject with key
class AService { }
const context = IocContext.DefaultInstance;
context.register(AService, 'XService');
context.get('XService');
use with decorators
const context = new IocContext();
@injectable()
class NRService { }
@injectable()
class LITestService {
@inject()
public testService: NRService;
}
const test = context.get(LITestService);
collect impl or extends of some interface/base class
class A { }
@classInfo()
class B extends A { }
@classInfo()
class C extends A { }
@injectable()
class LITestService {
@imports({ type: A })
public testService: A[];
}
const test = context.get(LITestService);
use in react
const context = new IocContext;
class NRService { }
context.register(NRService);
class TestComponent extends Component<{}, {}> {
@inject()
service: NRService;
componentDidMount() {
t.true(this.service instanceof NRService);
}
render(): any {
return null;
}
}
create(
<IocProvider context={context}>
<TestComponent />
</IocProvider>
);
Class Loader
Power DI introduced the concept of class loader. We can customize the find rule of ioc.