All Projects → mcordingley → Inverse.js

mcordingley / Inverse.js

Licence: mit
A dead-simple JavaScript IoC container. Easy to learn, easy to use.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Inverse.js

Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-97.02%)
Mutual labels:  ioc-container
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (-62.5%)
Mutual labels:  ioc-container
Microresolver
Extremely Fast Dependency Injection Library.
Stars: ✭ 138 (-17.86%)
Mutual labels:  ioc-container
Happysocialmedia
Microservices Social Media / Network / Chatt, with .net core 2.2, Docker, Implement with Domain Driven Design with all best practices design and architetural patterns as DDD, CrossCutting IoC, SOLID, etc
Stars: ✭ 28 (-83.33%)
Mutual labels:  ioc-container
Simplify
Simplify is a set of .NET libraries that provide infrastructure for your applications. DI and mocking friendly.
Stars: ✭ 52 (-69.05%)
Mutual labels:  ioc-container
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+616.07%)
Mutual labels:  ioc-container
Weaver
Dependency Injection framework for Swift (iOS/macOS/Linux)
Stars: ✭ 546 (+225%)
Mutual labels:  ioc-container
Core
CatLib lightweight dependency injection container
Stars: ✭ 148 (-11.9%)
Mutual labels:  ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (-66.07%)
Mutual labels:  ioc-container
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (-19.64%)
Mutual labels:  ioc-container
Servicemanager
🔌 Most basic implementation of dependency injection container for JavaScript
Stars: ✭ 29 (-82.74%)
Mutual labels:  ioc-container
Vue.js With Asp.net Core Sample
This provides a sample code using vue.js running on ASP.NET Core
Stars: ✭ 44 (-73.81%)
Mutual labels:  ioc-container
Spring Framework 4.2.0
spring源码学习附注释(Version 4.2.0),the second debug.
Stars: ✭ 87 (-48.21%)
Mutual labels:  ioc-container
Structuremap
A Dependency Injection/Inversion of Control tool for .NET
Stars: ✭ 877 (+422.02%)
Mutual labels:  ioc-container
Autofac.annotation
Autofac extras library for component registration via attributes 用注解来load autofac 摆脱代码或者xml配置和java的spring的注解注入一样的体验
Stars: ✭ 140 (-16.67%)
Mutual labels:  ioc-container
Learning laravel kernel
Laravel核心代码学习
Stars: ✭ 789 (+369.64%)
Mutual labels:  ioc-container
Minioc
Single-file minimal C# IoC container
Stars: ✭ 71 (-57.74%)
Mutual labels:  ioc-container
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (-4.76%)
Mutual labels:  ioc-container
Node Dependency Injection
The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
Stars: ✭ 140 (-16.67%)
Mutual labels:  ioc-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (-20.83%)
Mutual labels:  ioc-container

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].