1.0.1 • Published 9 years ago

adonis-ioc-container v1.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
9 years ago

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.

Features

  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.

v1.0.1

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{
    
  index(){
    return "Returning all users"
  }

}
Ioc.register("App/Controllers/User",User)

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")

console.log(UserController.index())
// @prints  Returning all users

Injecting into class constructor

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

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

}
Ioc.register("App/Models/UserModel",UserModel)

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

    constructor(UserModel){
      this.user = UserModel
    }

    index(){
      return this.user.fetch()
    }
}

Ioc.register("App/Controllers/UserController",UserController)

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

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

class Db{
   
   constructor(){
    this.db = null;
   }
   *boot(){
    try{
        this.db = yield makeDbConnection();
    }catch(e){
        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.

API

register new mapping to Ioc container

Ioc.register(name,class)

get mapping from container

Ioc.get(name)

store mapping as singleton to Ioc container

Ioc.singleton(name,class)

remove mapping from Ioc container

Ioc.remove(name)

map key/value pairs to Ioc container

Ioc.map(key,value)

destory all mappings

Ioc.destory()
1.0.1

9 years ago

1.0.0

9 years ago