GithubHelp home page GithubHelp logo

bwindels-nokia / container Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 2.0 143 KB

Dependency injection framework for javascript based on parameter names

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%

container's Introduction

container

Container is a library that allows you to specify dependencies, and then invoke functions that can use any of those previously specified dependencies. Dependencies are matched through the argument names of the function that is invoked. Dependencies can have dependencies of themselves, see the example below:

var Container = require('container').Container;
var container = new Container();

container.add('settings', {
	logFile: '/var/log/mylog.log'
});
container.add('logger', function(settings, callback) {
	var Logger = require('logger');
	return callback(null, new Logger(settings.logFile));
});

container.invoke(function(logger) {
	logger.info('inside invoked function');
});

###creating a container To start using dependency injection, the first thing to do is to create a container to add all your dependencies to.

var Container = require('container').Container;
var container = new Container();

###adding dependencies

Once having a container, you can add dependencies using container.add(name, value).

###Invoking functions order is not important, matching of arguments is done only by name, optional arguments have the postfix _optional. This means no error will be thrown when the argument name without the _optional postfix is not present in the container.

###API reference

####Container.add(name, value) Add a value to the container with name. value can be a creator function that returns the dependency value through a callback. The argument has to be called callback, and the creator function can specify any number of other arguments that should match other dependencies in the container. In other words, creator functions are also dependency injected, with a special extra argument called callback.

The callback accepts to arguments, the first being an error if one occured or null if not, and the second one the value of the dependency if no error occurred.

container.add('a', function(anotherDependency, callback) {
	callback(null, 4);
});

If value is not a function, the given value will be returned when the dependency is requested.

container.add('port', 8080);
container.add('encryptionKey', 'asd8f9asf787s8dff9s8d');
container.add('today', new Date(1918, 10, 11));

####Container.addAll(obj) Add multiple values to the container giving an object where each property is added to the container with the given name and value. Example:

container.addAll({
	a: 5,
	b: 3
});

####Container.addPath(name, path) ####Container.get(name, callback) retrieve a previously added dependency before running a callback function Example:

container.get('neededDependency', function(err, neededDependency) {
    if(err) {
        return err;
    }
    container.on('error', function(err) {
        console.log('ERROR in DI container: ' + err.stack);
    });
});

####Container.invoke(fn [, extraArguments], callback) invoke a function handing in a needed dependency Example:

container.invoke(function(neededDependency) {
    a(neededDependency);
}, callback);

####Container.invokeAll(obj [, extraArguments], callback) ####Container.bind(fn [, extraArguments], callback) ####Container.bindAll(obj [, extraArguments], callback) ####Container.create(path, callback) ####Container.createAll(obj, callback)

####error event on container

###circular reference detection

###future plans

  • add way to obtain dependency graph so it can be drawn in a diagram.

container's People

Stargazers

 avatar

Watchers

 avatar

Forkers

bwindels chrkaatz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.