NODEJS(17)Platform - DI

本文深入探讨了Node.js中依赖注入(DI)的概念及其应用实践,包括DI的基本介绍、使用示例、依赖注入框架Dependable的实现方式,并提供了扩展对象继承的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NODEJS(17)Platform - DI
 
1. DI Introduction
a. Some consumer, this is the thing that has dependencies
b. Some dependencies. These are the things that the consumer needs in order to work.
c. Some Injector. The injector is the thing that connects the consumer to its dependencies.
 
Take a look at this Examples
var config = require(‘../config.json’);
var User = require(‘./models/user’);
var Group = require(‘./models/group’);
var Post = require(‘./models/post’);
 
var Client = require(‘./lib/client’);
 
var client = Client(config);
var user = User(client);
var group = Group(client);
var post = Post(client);
 
In this example, the consumers are our models
The dependency is our client.
The injector is the part where we manually require everything and pass the configured parts along.
 
Dependency Injection Framework
The framework which i.TV wrote is Dependable.
 
var container = require(‘dependable’).container;
var User = require(‘./models/user’);
var Group = require(‘./models/group’);
var Post = require(‘./models/post’);
 
var Client = require(‘./lib/client’);
 
container.register(‘config’, require(‘../config.json’));
container.register(‘client’, function(config) {
     return Client(config);
});
 
container.register(‘user’,User);
container.register(‘group’,Group);
container.register(‘post’, Post);
 
container.resolve(function(user, group, post) {
 
});
 
2. Read the Source codes in Dependable
> sudo npm install -g coffee-script
> coffee --compile index.coffee
 
 
3. Some Basic Tips
Array.prototype.slice.call
Array [Object] 
prototype [property]  
slice [Function]  Something like substring for string, slice for array
call [Function]
 
Array.prototype.slice.call == [].slice
Array.prototype.slice.call(obj, begin, end) = Array.slice(begin, end)
 
Array.prototype.every()
arr.every(callback[, thisArg])
 
function isBigEnough(element, index, array){
     return (element >= 10);
}
 
var passed = [12,5,8,130,44].every(isBigEnough);
//false
passed = [12,54,18,130,44].every(isBigEnough);
//true
 
Function.prototype.apply()
fun.apply(thisArg, [argsArray])
 
ObjectA.apply(this, arguments); —————> ObjectA.call(this);
 
apply can extends other object, inherit their methods and properties.
 
Example to Extends
function Person(name, age){
     this.name=name;
     this.age=age;
}
function Student(name,age,grade){
     Person.apply(this,arguments);
     this.grade=grade;
}
var student = new Student(“zhangsan”,21,”grade 1”);
alert(“name:”+student.name+”\n”+”age:”+student.age+”\n”+”grade:”+student.grade);
 
———>
Person.call(this,name,age);
 
4. I Plan to Take Reference and Write a DI
 
I just use hashmap to store the servers I created right now.
 
 
 
References:
Open Source DI
 
Blogs
 
 
angularjs DI
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值