Giter VIP home page Giter VIP logo

javascript-dependency-injection's Introduction

Get this with Bower

Build Status Coverage Status Dependency Status devDependency Status Code Climate Inline docs

Javascript Dependency Injection library

DI makes classes accessible by a contract. Instances are created when requested and dependencies are injected into the constructor, facilitating lazy initialization and loose coupling between classes.

As an example, consider a User and Persitance classes:

function WebSql(name, fieldList)  {
    this.persist = function (obj) {
        console.log('WebSQL will persist:');
        fieldList.forEach(function (field) {
            console.log('    ' + field + ': ' + obj[field]);
        });
    }
}

function IndexDB(name, fieldList)  {
    this.persist = function (obj) {
        console.log('IndexDB will persist:');
        fieldList.forEach(function (field) {
            console.log('    ' + field + ': ' + obj[field]);
        });
    }
}

function User(email, passwd, storage, role) {    // the `storoge` parameter holds an instance
    this.email = email;
    this.passwd = passwd;
    this.role = role;

    this.save = function () {
        storage.persist(this);
    };
}

With these classes in our pocket its time to setup the relations between them. The function that does this has the following signature

 function (<contract name>, 
           <class reference>, 
           [optional list of constructor arguments], 
           {optional configuration object} ) 

Or just in code:

var di = new DI();

di.register('user', User, [null, 'welcome', 'websql', 'nobody']);
di.register('websql', WebSql, ['userTable', ['email','passwd', 'role']], {singleton: true});
di.register('indexdb', IndexDB, ['userTable', ['email','passwd', 'role']], {singleton: true});

Note that the provied constructor arguments are default values or contract names. From now it is easy to create instances:

var user1 = di.getInstance('user', ['[email protected]']),
        -> email: '[email protected]', passwd: 'welcome', storage : WebSQL instance, role: 'nobody'
    user2 = di.getInstance('user', ['[email protected]', 'newSecret']); // define a new password
        -> email: '[email protected]', passwd: 'newSecret', storage : WebSQL instance, role: 'nobody'
        
if (user1 instanceof User) { /* user1 is an instance of User!! */ }

But it is also possible to use IndexDB as the persistance class:

var user = di.getInstance('user', ['[email protected]', null, 'indexdb']),               // The password is set to null too!
        -> email: '[email protected]', passwd: null, storage : IndexDB instance, role: 'nobody'
    root = di.getInstance('user', ['[email protected]', undefined, 'indexdb', 'admin']); 
        -> email: '[email protected]', passwd: 'welcome', storage : IndexDB instance, role: 'admin'

Checkout more detailed the documentation here

Gulp tasks

Install the dependencies as follows

$> npm install

To minify the library

$> gulp

To run the tests

$> gulp test

Installation

$> bower install javascript-dependency-injection

javascript-dependency-injection's People

Contributors

scaljeri avatar

Watchers

 avatar

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.