NODEJS(17)Platform
NODEJS(17)Platform - DI1. 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.
https://github.com/idottv/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
>git clone https://github.com/idottv/dependable.git
>sudo npm install -g coffee-script
>coffee --compile index.coffee
3. Some Basic Tips
Array.prototype.slice.call
Array
prototype
slice Something like substring for string, slice for array
call
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 = .every(isBigEnough);
//false
passed = .every(isBigEnough);
//true
Function.prototype.apply()
fun.apply(thisArg, )
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
https://github.com/idottv/dependable
https://github.com/harmonyjs/dependency-injection
Blogs
http://blog.nodeside.com/nodejs-dependency-injection-example
http://krasimirtsonev.com/blog/article/Dependency-injection-in-JavaScript
http://www.joezimjs.com/javascript/dependency-injection-with-node-js/
http://tech.i.tv/09-17-2013/dependency-injection-in-nodejs/
http://blog.csdn.net/lmstone/article/details/3050742
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
http://www.codebit.cn/javascript/javascript-every.html
http://www.111cn.net/wy/js-ajax/javascript-every.htm
http://blog.csdn.net/myhahaxiao/article/details/6952321
angularjs DI
http://www.cnblogs.com/asnowwolf/p/3684700.html
https://github.com/angular/di.js
https://docs.google.com/document/d/1fTR4TcTGbmExa5w2SRNAkM1fsB9kYeOvfuiI99FgR24/edit
https://www.npmjs.com/package/loadtest
页:
[1]