Adonis ioc container is the real glue behind the adonis framework, it works as a peer dependency to register packages to adonis
Last updated 5 years ago by amanvirk .
ISC · Original npm · Tarball · package.json
$ cnpm install adonis-ioc-container 
SYNC missed versions from official npm registry.

IOC Container

NodeJs Ioc container that plays well with ES6 and makes DI more fun. It comes with a service provider , which makes it easy to make plugins/packages and let them be dependent upon each other.

Ioc ( Inversion of Control )

IOC container is a term to inject dependencies and make testing process easier by giving you a chance to Mock dependencies.


  1. Register Es6 classes to container.
  2. Inject dependencies right into your constructor.
  3. Map key/value pairs to injector.
  4. Service provider to help you make plugins.


Added support for aysnc boot method.

Ioc container auto resolves dependencies, at times your dependencies have to do some work before they are ready to be used.

For same, each class have to register a boot method which get invoked automatically during DI lifecycle.

Now boot methods are simple es6 generators which makes it easier to manage asynchronous behavor.

Basic Example

let Ioc       = new(require("adonis-ioc-container").Ioc)

// User controller
class User{
    return "Returning all users"


It is always nice to namespace your dependencies, because there can be multiple User classes.

Using Above Injection

let UserController = Ioc.get("App/Controllers/User")

// @prints  Returning all users

Injecting into class constructor

let Ioc       = new(require("adonis-ioc-container").Ioc)

// User Model
class UserModel{
    return "I will fetch from db"


// User Controller
class UserController{
    static get inject() {
      return ["App/Models/UserModel"]

      this.user = UserModel

      return this.user.fetch()


In above example we inject namespaced model App/Models/UserModel to our controller , which in-turn makes it easier for you to mock it while testing.

Lifecycle events

Lifecycle events gives a chance to do all heavy lifting as by then Ioc container injection cycle is stable and your class is good to go.


boot method on your class will called automatically after all injections are stable. It has to be a generator method.

class Db{
    this.db = null;
        this.db = yield makeDbConnection();
        throw e;

Testing Example For Above Scanerio

let UserController = new UserController(MockedUserModel)

As we inject dependencies right into the constructor, it is easy to mock them while testing.


register new mapping to Ioc container


get mapping from container


store mapping as singleton to Ioc container


remove mapping from Ioc container


map key/value pairs to Ioc container,value)

destory all mappings


Current Tags

  • 1.0.1                                ...           latest (5 years ago)

2 Versions

  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 1
Last Month 2
Dependencies (1)
Dev Dependencies (2)
Dependents (0)

Copyright 2014 - 2016 © |