Code Monkey home page Code Monkey logo

inverse.js's Introduction

Inverse.js

A dead-simple JavaScript IoC container in 50 lines. Yes, you read that right: 50 lines. The primary purpose of an Inversion of Control container is to serve as a location to place application-specific code that creates objects. It's a registry of factories, effectively. The other JavaScript solutions to this problem complicate the matter unnecessarily with their own formats for registering factory methods and pages of documentation. Getting up and running with them is a project in itself. It shouldn't be this way.

API

To start out, create an instance of the container:

var container = new Inverse();

Container instances are kept as objects, so you can either inject them as dependencies to other objects of yours or set one up in your application as a global, whichever works better for you.

bind

With your container in hand, you can register a factory function with the bind function:

container.bind(name, callback);

Just provide it with a name and the callback that you intend to use with it. When this callback is called, it will be provided the container instance as its this value. This enables bound factory functions to then use the container to create their own dependencies. The callback is then provided with any additional arguments that were given to make.

singleton

You can also register a factory function that will only ever be called once with the singleton function. Subsequent calls to make this object will return the object created with the first call. This is useful for things such as objects representing connections to third-party systems.

container.singleton(name, callback);

The behavior is otherwise identical to bind.

register

If you have already created the object or have had it created for you by third-party code, it can be registered directly with register. Like singleton, the same object instance will be returned each time the object is requested.

container.register(name, object);

make

With your factory functions in place, just call make to get your object. Any additional arguments beyond the key name of the binding are passed on to the constructor function.

container.make(name);

Example

To get started, we'll need an instance of the IoC container:

var container = new Inverse();

Good! Now, we have a container that can be used to register new bindings into. Next, I'm going to be an awful person and put this variable into the global namespace. In my imaginary world, I'm in the browser, so I'll do it as follows:

window.container = container;

You probably want to be all fancy and inject this as a dependency into your objects that use it, but whatever.

Suppose you're using some third-party web service, we'll call it ComplicAPI, that requires a few configuration options. Every time you need this connection to ComplicAPI, you'll need to grab the configuration settnigs for it and build it fresh. That means that the same block of code will exist in multiple places in your code base. That's not very dry! Instead, register that block of code into a singleton on your container:

container.singleton('complicapi-connection', function() {
    return new ComplicAPI.Connection('user-dude', {
        aync: true,
        apiVersion: 5,
        logLevel: 'info',
        printMoogleToConsole: true
    });
});

Now, every place that you need an instance of the ComplicAPI connection, instead of going through that rigamorale, just get it from the container.

var connection = container.make('complicapi-connection');

As a bonus, you're using the same connection everywhere and you're only creating the connection when it's needed. Neat! What about more complicated objects that shouldn't be unique? That's not much harder. ComplicAPI has some objects that you use in your code, but they all need a reference to the connection. Unlike the connection, though, each instance is a special, unique snowflake. OK, then. Let's set up the binding for that new object type.

container.bind('snowflake', function(radius) {
    return new ComplicAPI.Snowflake(container.make('complicapi-connection'), {
        radius: radius
    });
});

Now you can easily get all of the snowflakes that you want. You can have a ball with them!

container.bind('snowball', function() {
    var flakes = [];
    
    for (var i = 0; i < 1000000; i++) {
        flakes.push(container.make('snowflake', Math.rand()));
    }
    
    return new ComplicAPI.Snowball(flakes);
});

Change Log

  • 1.0.0

    • Marking stable with no changes.
  • 0.10.0

    • Moved the container instance passed to callbacks from being their first argument to being this for them.
    • Passing along additional arguments to make on into the callbacks as their argument list.

inverse.js's People

Contributors

mcordingley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

inverse.js's Issues

Dependencies?

Nice project, I love the simplicity of it. That said, are you planning on adding any sort of dependency system? Or just keeping things super simple?

It would also be nice to publish it to npm

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.