npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

adonis-ioc-container

v1.0.1

Published

Adonis ioc container is the real glue behind the adonis framework, it works as a peer dependency to register packages to adonis

Downloads

6

Readme

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