Skip to content

Framework-agnostic dependency container

License

Notifications You must be signed in to change notification settings

aldojs/container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A framework-agnostic dependency container.

Install

$ npm add @aldojs/container

Usage

let { createContainer } = require('@aldojs/container')

let container = createContainer()

// add a `foo` factory
container.bind('foo', (c) => ({ foo: true }))

// later, create `foo` object
let obj = container.make('foo')

API

container.bound(name)

Check a bind is already defined.

container.bind(name, fn)

Add an new factory into the container.

container.singleton(name, fn)

Bind a singleton factory to make a singleton instances.

container.make(name, ...args)

Create and return the service instance, passing the additional arguments.

Factory function

Each function given to bind or singleton as second parameter should have the following signature

function Factory = (c: Container, ...args: any[]) => any;