0.1.1 • Published 8 years ago

hypodermic-js v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

Hypodermic

A portable dependency injection library for JavaScript.

Installing

To use this library in you project with npm

npm install hypodermic-js

and you can import it as a module with

require('hypodermic-js')

Or simply download from github and include ./dist/hypodermic.js into your scripts.

Registering Dependencies

Hypodermic allows you to register either a class, factory or value. Registered dependencies get created as a singleton instance, persisting state across many resolution contexts.

Registering a class:

Registering a class stores a new'd instance of the class.

    function MyClass(){}

    //creates and stores a new instance of MyClass as 'RegistrationName'
    Hypodermic.registerProvider({
        provide: 'RegistrationName',
        useClass: MyClass
    });

You can also register a class by default by passing only the class

    function MyClass(){}
    
    //creates and stores a new instance MyClass as 'MyClass'
    Hypodermic.registerProvider(MyClass);

Registering a factory:

Registering a factory executes the factory function and stores the result under the registered name.

    Hypodermic.registerProvider({
        provide: 'CreatedObject',
        useFactory: function(){
            if(config.production){
                return new ProductionLogger();
            }
            else{
                return new NullLogger();
            }
        }
    })

Registering a value:

You can also register static values.

    var configData = {
        environment: 'test',
        key: '3DFA-121A-DDE4'
    }
    
    Hypodermic.registerProvider({
        provide: 'config',
        useValue: configData
    })

Registering a dependency with dependencies

Registered classes and factories can be instantiated with other dependencies that are registered in hypodermic

In this example we have already regisered a provider called 'UserRepository'

    function UserService(userRepository){
        this.userRepository = userRepository;
    }
    
    //Assuming we have already registered UserRepository we can specify the deps property
    //and the UserService will be new'd with that dependency injected into the constructor
    Hypodermic.registerProvider({ 
        provide: 'UserService', 
        useClass: UserService, 
        deps:['UserRepository']
    })

Resolving Dependencies

Resolving objects

    //Assuming we have already registered a class called UserService
    var service = Hypodermic.resolve('UserService')

Injecting into function

    //Assuming we have already registered a class called UserService
    Hypodermic.inject(['UserService'], function(service){
        //service is the registered instance of UserService because of inject
        service.getData();
    })

Contributing

If you have any ideas or find any issues, leave an entry in the issues tab of Github. If you would like to contribute, fork, and make a pull request, add a good description outlining your changes and I will review it ASAP. Make sure to add unit tests around your contributions.