@aantthony/ioc 中文文档教程
ioc
Installation
npm install --save "@aantthony/ioc"
Usage
const ioc = require('@aantthony/ioc');
const createLogger = require('@aantthony/logger');
function createApplication() {
return ioc({
log: ioc.transient(module => createLogger(module.name)),
x() { return 1; },
y() { return 2; },
z() { return 3; },
example(x, y, z) {
return x + y + z;
},
demo(example, log) {
return {
run() {
log.info('Example = ' + example);
}
}
},
// anotherModule: require('./another-module'),
});
};
module.exports = createApplication();
// 2017-12-04 11:15:47 demo: Example = 6
createApplication().demo.run();
Built in modules:
module
描述引用模块的对象。 注意:如果服务依赖于 module
,则必须将其显式声明为 ioc.transient()
,如上面的示例代码所示。
{ name: 'exampleModule' }
inject
可用于注入依赖项的函数。
// Executes the function with the dependencies injected.
inject((moduleA, moduleB) => {
moduleA.doSomething();
moduleB.doSomethingElse();
});
ioc
Installation
npm install --save "@aantthony/ioc"
Usage
const ioc = require('@aantthony/ioc');
const createLogger = require('@aantthony/logger');
function createApplication() {
return ioc({
log: ioc.transient(module => createLogger(module.name)),
x() { return 1; },
y() { return 2; },
z() { return 3; },
example(x, y, z) {
return x + y + z;
},
demo(example, log) {
return {
run() {
log.info('Example = ' + example);
}
}
},
// anotherModule: require('./another-module'),
});
};
module.exports = createApplication();
// 2017-12-04 11:15:47 demo: Example = 6
createApplication().demo.run();
Built in modules:
module
An object which describes the referring module. NOTE: If a service depends on module
, it must be explicitly declared as ioc.transient()
as is shown in the above example code.
{ name: 'exampleModule' }
inject
Function which can be used to inject dependencies.
// Executes the function with the dependencies injected.
inject((moduleA, moduleB) => {
moduleA.doSomething();
moduleB.doSomethingElse();
});