node-ninject
nodejs端的DI库,可以方便的在类的构造函数中注入依赖
{ thistestService = testService; // testService => new TestServie() } { thistestService; };
Getting Started
Installation
npm install node-ninject
or
yarn add node-ninject
API
Ninject
init(config: ninjectConfig)
config: ninject的映射配置
createInstance(type: Type, [injects: Object])
type: 类型
injects:注入到实例化链中的上下文对象(注入后当前实例化对象和所有依赖关系的实例化对象都拥有该上下文,在nodejs中传递请求上下文很有用)
NinjectConfig
数组对象,维护类型和别名的映射,ninject从中读取实例化的类型。
{alias: 'testService', bind: TestService}
Usage
ninject-config.js
const TestService = ;const DepService = ;const Dep2Service = ;moduleexports =alias: 'testService' bind: TestServicealias: 'depService' bind: DepServicealias: 'dep2Service' bind: Dep2Service;
test-controller.js
moduleexports ={thistestService = testService;}{thistestService;};
index.js
const ninject = ;const ninjectConfig = ;const TestController = ;ninject;const context =req:url: 'http://xxx.com'res: {};tryconst testController = ninject;testController;catch errorconsole
License
node-ninject is licensed under a MIT License.