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.
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’);
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
angularjs DI
http://www.cnblogs.com/asnowwolf/p/3684700.html
https://github.com/angular/di.js
https://docs.google.com/document/d/1fTR4TcTGbmExa5w2SRNAkM1fsB9kYeOvfuiI99FgR24/edit