Coder Social home page Coder Social logo

enjection's Introduction

Dependency Injection

When developing across modules, dependencies are normally introduced by the require statement. This method has a drawback of tying the modules together by a specific relative (or even absolute) path, and makes testing more difficult since each module is tied to others.

There are existing Dependency Injection system out, however each one of them requires listing the necessary dependencies (usually by string) and then a number of references that the dependencies will be received in.

enjection solves this issue. New modules are defined as a function that accepts a dependency object, and dependencies are recursively resolved as they are accessed off the dependency object.

This means you can define what dependencies all of your modules will use based on whether you are in development, production, testing, or more. Also, each module does not need to know where another is to require it as a dependency.

Usage

When starting up an application, you only need to register new modules and then execute one. If a module is defined as an Enjection module, register is used. If a module is a standard module (ex. node_modules) the inject function is used. Finally, resolve is used to start resolving one of the modules.

entry.js

const { inject, register, resolve } = require('enjection');

inject('express', require('express'));
inject('http', require('http'));
inject('logger', require('some-logger'))

register('app', require('./app'));
register('createEndpoint', require('./create-endpoint'));

const server = resolve('app');

New modules are defined as either singletons or factories:

app.js

// Singleton module
module.exports = ({ express, http, createEndpoint })=> {
    const app = express();

    app.get('/', createEndpoint('Hello, Home Page!'));
    app.get('/contact', createEndpoint('Hello, Contact Page!'));
    app.get('/pricing', createEndpoint('Hello, Pricing Page!'));
    app.use((req, res)=> res.send('Not Found'));

    return http.createServer(app);
};

create-endpoint.js

// Factory module (just returns a function)
module.exports = ({ logger })=> (text)=> {
    return (req, res)=> {
        logger.info(text);
        res.send(text);
    };
};

Full API

inject(String name, Any module)
injectAll(Object modules)
register(String name, Func DIModule)
resolve(String name)
reset()

enjection's People

Contributors

tylerbwilliams avatar

Stargazers

 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.