A lightweight IoC container inspired by Martin Fowler's article on Inversion of Control Containers and the Dependency Injection pattern. http://martinfowler.com/articles/injection.html
###Get it here https://raw.githubusercontent.com/kat3samsin/liteinjector/master/liteinjector.js
#Getting Started
###Setup
Create an abstract class.
function AbstractDataSource() {
}
AbstractDataSource.prototype.getData = function() {
};
Create different concrete implementations.
function SqlDataSource() {}
SqlDataSource.prototype = Object.create(AbstractDataSource.prototype);
SqlDataSource.prototype.getData = function() {
return 'SqlDataSource';
}
function AnotherDataSource() {}
AnotherDataSource.prototype = Object.create(AbstractDataSource.prototype);
AnotherDataSource.prototype.getData = function() {
return 'AnotherDataSource';
}
Create a controller.
function DataSourceController(ds) {
this.ds = ds;
}
DataSourceController.prototype.getData = function() {
return this.ds.getData();
};
###Putting it all together
Add a dependency. Use controller argument as the first parameter. (key)
liteinjector.addDependency('ds', new SqlDataSource());
Get object with the injected dependency.
var ds = liteinjector.get(DataSourceController);
ds.getData(); //SqlDataSource
Add a new dependency.
liteinjector.addDependency('ds', new AnotherDataSource());
var ds = liteinjector.get(DataSourceController);
ds.getData(); //AnotherDataSource
Get full sample source here: https://raw.githubusercontent.com/kat3samsin/liteinjector/master/sample.js