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
>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 [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
相关推荐
基于vue+nodejs毕业设计-在线购物商城系统(源码+数据库+文档说明)含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用,该项目可以作为毕设、期末大作业使用,...
cos-nodejs-sdk-v5腾讯云 COS Nodejs SDK()installnpm i cos-nodejs-sdk-v5 --savedemo// 引入模块var COS = require('cos-nodejs-sdk-v5');// 创建实例var cos = new COS({ SecretId: '...
基于vue+nodejs毕业设计-图书管理系统(源码+论文资料),该项目是个人毕设项目,答辩评审分达到98分,代码都经过调试测试,确保可以运行!欢迎下载使用,可用于小白学习、进阶。该资源主要针对计算机、通信、人工...
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs010-nodejs-request-2.25.0-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-qs-0.6.5-3.el6.centos.alt.noarch.rpm
nodejs010-nodejs-npmlog-0.0.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-slide-1.1.5-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-promzard-0.2.0-7.el6.centos.alt.noarch.rpm
nodejs010-nodejs-npmconf-0.1.12-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-rimraf-2.2.6-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-nan-0.4.4-2.2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-sntp-0.2.4-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-hoek-0.9.1-2.el6.centos.alt.noarch.rpm
nodejs010-nodejs-minimatch-0.2.14-1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-sha-1.2.1-1.1.el6.centos.alt.noarch.rpm
nodejs010-nodejs-inherits-2.0.0-6.el6.centos.alt.noarch.rpm