All Projects → cmtt → Dijs

cmtt / Dijs

Licence: mit
JavaScript dependency injection for Node and browser environments.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Dijs

Alki
Dependency Injection for Ruby
Stars: ✭ 27 (-44.9%)
Mutual labels:  dependency-injection
Injection Js
Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.
Stars: ✭ 962 (+1863.27%)
Mutual labels:  dependency-injection
Social Note
Social Note - Note-taking, sharing, time & location reminder
Stars: ✭ 38 (-22.45%)
Mutual labels:  dependency-injection
Servicemanager
🔌 Most basic implementation of dependency injection container for JavaScript
Stars: ✭ 29 (-40.82%)
Mutual labels:  dependency-injection
Autowire
Lightweight & Simple dependency injection and resource management library for Python
Stars: ✭ 31 (-36.73%)
Mutual labels:  dependency-injection
Core
Package core is a service container that elegantly bootstrap and coordinate twelve-factor apps in Go.
Stars: ✭ 34 (-30.61%)
Mutual labels:  dependency-injection
Codewif
An Android framework that provides in-app testing
Stars: ✭ 15 (-69.39%)
Mutual labels:  dependency-injection
Vue.js With Asp.net Core Sample
This provides a sample code using vue.js running on ASP.NET Core
Stars: ✭ 44 (-10.2%)
Mutual labels:  dependency-injection
Graphql Modules
Enterprise Grade Tooling For Your GraphQL Server
Stars: ✭ 962 (+1863.27%)
Mutual labels:  dependency-injection
Nex
Aiming to simplify the construction of JSON API service
Stars: ✭ 35 (-28.57%)
Mutual labels:  dependency-injection
Lean
Use the PHP League's Container package with auto-wiring support as the core container in Slim 3
Stars: ✭ 30 (-38.78%)
Mutual labels:  dependency-injection
Flair
This is powerful android framework
Stars: ✭ 31 (-36.73%)
Mutual labels:  dependency-injection
Inversifyjs
InversifyJS is a lightweight inversion of control (IoC) container for TypeScript and JavaScript apps. An IoC container uses a class constructor to identify and inject its dependencies. InversifyJS has a friendly API and encourages the usage of the best OOP and IoC practices.
Stars: ✭ 8,399 (+17040.82%)
Mutual labels:  dependency-injection
Spring Depend
Tool for getting a spring bean dependency graph
Stars: ✭ 28 (-42.86%)
Mutual labels:  dependency-injection
Gulp Di
gulp-di is a dependency injection framework for the Gulp streaming build system.
Stars: ✭ 40 (-18.37%)
Mutual labels:  dependency-injection
Mydi
moved to https://github.com/cekta/di
Stars: ✭ 21 (-57.14%)
Mutual labels:  dependency-injection
Simpleinjector
An easy, flexible, and fast Dependency Injection library that promotes best practice to steer developers towards the pit of success.
Stars: ✭ 966 (+1871.43%)
Mutual labels:  dependency-injection
Ios
A sample project demonstrating MVVM, RxSwift, Coordinator Pattern, Dependency Injection
Stars: ✭ 49 (+0%)
Mutual labels:  dependency-injection
Zenject Hero
Zenject 7 - Game example (WIP)
Stars: ✭ 44 (-10.2%)
Mutual labels:  dependency-injection
Picobox
Dependency injection framework designed with Python in mind.
Stars: ✭ 35 (-28.57%)
Mutual labels:  dependency-injection

dijs

dijs is a dependency injection framework for Node.js and browser environments.

It is inspired by AngularJS 1.x and allows to choose between synchronous and asynchronous (using callbacks and promises) resolution patterns in an non-opinionated way.

Featured on DailyJS.

Example

  const Di = require('dijs');

  class TestClass {
    constructor (PI, RAD_TO_DEG) {
      this.PI = PI;
      this.RAD_TO_DEG = RAD_TO_DEG;
    }

    deg (value) {
      return value * this.RAD_TO_DEG;
    }
  }

  // Initialize a new dijs instance. By default, this will use "CallbackMethod",
  // thus the first argument is "null" (instead providing another method).
  // The $provide and $resolve method expect callback functions.

  let instance = new Di(null)
  // Provides a constant
  .$provideValue('PI', Math.PI)
  // Providing a constant using dependencies
  .$provide('RAD_TO_DEG', (PI, callback) => callback(null, (180 / PI)))
  .$resolve((err) => {
    if (err) {
      throw err;
    }
    let AnnotatedTestClass = instance.$annotate(TestClass);
    let a = new AnnotatedTestClass();

    // logs 180
    console.log(a.deg(Math.PI));
  });

Options

assign

By default, all provided values are being set on the dijs instance. This behavior can be turned off:

  const Di = require('dijs');
  let d = new Di(null, 'Math', { assign : false });

  d.$provide('PI', function (callback) {
    callback(null, Math.PI);
  });
  d.$resolve(function (err) {
    if (err) {
      throw err;
    }
    console.log(`d.PI is undefined`, d.PI === undefined);
    console.log(`d.$get("PI") equals Math.PI`, d.$get('PI') === Math.PI);
  });
});

Usage

new Di(Method, name, options)

Returns a new dijs instance with the given method (CallbackMethod is the default one).

Instance methods

$annotate(classFn)

Returns a class which will be initialized with the dependencies stated in classFn's constructor. When classFn is a function, its parameters are being resolved to matching dependencies.

This method must called after $resolve().

$get(id)

Returns the (previously provided) sub-module specified by a dot-delimited id.

$inject(arg)

Calls a function with (already-provided) dependencies. It is required to call $resolve beforehand.

Dependant on the current resolution method, this function is synchronous or asynchronous

$provide(key, object, passthrough)

Provides a module in the namespace with the given key.

If passthrough is set, the object will be just passed through, no dependencies are looked up this way.

$provideValue(key, object)

Provides a value in the namespace with the given key (shortcut for $provide using passthrough).

$resolve()

Resolves the dependency graph.

This method might take a callback function (in case of the default CallbackMethod) or return a promise with PromiseMethod.

$set(id, value)

Sets a value in the namespace, specified by a dot-delimited path.

Resolution methods

By default, dijs uses the asynchronous CallbackMethod in order to resolve dependencies. Require them as follwing:

const Di = require('dijs');
const SyncMethod = require('dijs/methods/sync');
const PromiseMethod = require('dijs/methods/promise');

CallbackMethod

Asynchronous providing and resolving dependencies using Node.js-style callback functions.

It is expected that the last parameter of your callback functions is called "callback", "cb" or "next".

This function takes an error (or a falsy value like null) as first argument. The second argument should be the provided value.

  let d = new Di();
  d.$provide('PI', (callback) => { // alternative names: cb or next
    callback(null, Math.PI);
  });

  d.$resolve((err) => {
    if (err) {
      return done(err);
    }
    assert.equal(d.PI, Math.PI);
    done();
  });

PromiseMethod

Provides and resolves dependencies using the ES2015 Promise API.

  let d = new Di();
  d.$provide('PI', Promise.resolve(Math.PI));
  d.$provide('2PI', (PI) => Promise.resolve(2 * Math.PI));
  d.$resolve().then(() => {
    assert.equal(d['2PI'], 2 * Math.PI);
    done();
  }, (err) => {
    done(err);
  });

SyncMethod

Synchronous way to provide and resolve depdencies.

  let d = new Di(SyncMethod);
  d.$provide('PI', Math.PI);
  d.$provide('2PI', (PI) => 2 * Math.PI);
  d.$resolve();
  assert.equal(d['2PI'], 2 * Math.PI);

Reference

Notation

If you don't pass a value through, you can choose between the function and the array notation to describe the module's dependencies. In any case, you will need to pass a function whose return value gets stored in the namespace. Its parameters describe its dependencies.

function notation

To describe a dependency, you can pass a function whose parameters declare the other modules on which it should depend.

Note: You cannot inject dot-delimited dependencies with this notation.

  var mod = new Di();
  mod.$provide('Pi',Math.PI, true);
  mod.$provide('2Pi', function (Pi, callback) { callback(null, return 2*Pi); });
  // ...

array notation (minification-safe)

When your code is going to be minified or if you are about to make use of nested namespaces, the array notation is safer to use. All dependencies are listed as strings in the first part of the array, the last argument must be the actual module function.

  var mod = new Di();
  mod.$provide('Math.Pi',Math.PI, true);
  mod.$provide('2Pi', ['Math.Pi', function (Pi, callback) {
    callback(null, 2*Pi);
  }]);
  // ...

notation with "$inject" (minification-safe)

An alternative way is to provide a "$inject" property when using $annotate or $provide. This requires minification tools being in use not to mangle this key:

  class TestClass {
    constructor (pi) {
      this.RAD_TO_DEG = (180 / pi);
    }

    deg (value) {
      return value * this.RAD_TO_DEG;
    }
  }

  TestClass['$inject'] = ['PI'];

  var mod = new Di();
  mod.$provideValue('PI', Math.PI);
  mod.$resolve((err) => {
    if (err) { throw err; }
    let WrappedTestClass = mod.$annotate(TestClass);
    let instance = new WrappedTestClass();
    console.log(instance.deg(Math.PI * 2)); // 360
  });

Namespacing

Each dijs instance has a new namespace instance at its core. Namespaces provide getter/setter methods for sub-paths:

  var Namespace = require('dijs/lib/namespace');
  var namespace = new Namespace('home');
  namespace.floor = { chair : true };
  assert.deepEqual(namespace.$get('home.floor'), { chair : true})
  namespace.$set('home.floor.chairColor', 'blue');
  assert.deepEqual(namespace.floor, { chair: true, chairColor: 'blue' });

Please note that dijs assigns all namespace values to instances. You can disable this behavior using the "assign" option (see above).

Usage in the browser

Bundled versions are available in the dist/ folder.

You can create a minified build with Google's Closure compiler by running make in the project directory.

License

MIT (see LICENSE)

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].