overdose

Dependency injection framework and Inversion of Control container

Usage no npm install needed!

<script type="module">
  import overdose from 'https://cdn.skypack.dev/overdose';
</script>

README

Overdose

Overdose is a Dependency Injection mechanism that uses an Inversion of Control container to inject dependencies into Objects. The container enabled decoupling of modules and the dependencies of the module to be injected when run.

Objects can be registered with the container and the container can be told what dependencies the Object needs.

Install

Installation goes through npm:

$ npm install overdose

Overdose does not have any dependencies except for running the tests.

Test

Tests are written using tap and can be run with npm if all dev-dependencies are installed:

$ npm install overdose
$ npm test

If you want to run individual tests use this, where name is substituted for the test name:

$ node test/{NAME}.test.js

License

This module is licensed under the MIT License. See the LICENSE file for more details.

API

Container

constructor

register(service, definition)

load(name, definition)

provide(provider, definition)

get(name)

has(name)

inject(dependency, depender, assign, force)

Load definition

keys

introspection

Examples

Create a IOC container

var Container = require('overdose').Container;
var container = new Container();

Register a service

Overdose exposes several ways of registering services with the container. Depending on the situation and the requirements of that situation one will fit better than the other.

There are three ways to register a service:

  • Using register for existing (instantiated) services
  • Using load to register a service from a module
  • Using provide to register a service that is instantiated by a provider function.
var myService = {
    foo: "bar"
};
container.register(myService, 'foo');

Loading services from a module

Using a constructor:

var httpServer = container.load({
  "name":        "httpServer",
  "module":      "http",
  "constructor": "Server"
});

Using a factory function:

var httpServer = container.load({
  "name":    "httpServer",
  "module":  "http",
  "factory": "createServer"
});