If a registration indicates it is for a singleton, the item may not be unregistered.

Injection

minioc performs dependency injection on both factories and constructors. Its convention is to inject any argument whose name begins with a dollar sign ($); other named arguments must be caller-supplied.

The container distinguishes between items that it can fulfill entirely from those that are registered but not fully resolvable.

Tests

API

minioc

When you import the minioc module, the resulting object is a constructor for the Container class, but it also provides several convenience methods that can be used directly.

can - property- determines whether an item can be resolved with all dependencies.

create - function - creates a new nested container.

get - function - gets an item according to its registration with the root container.

has - function - determines if an item has a registration with the root container.

register - function - registers a named item with the root container.

fulfill - function - registers a callback function with the root container to be invoked when the function's arguments can be fulfilled.

root - property - provides access to the root container.

when - function - registers a callback invoked when an item can be resolved with all dependencies.

Container

Containers are nested. The root container always exists and most registrations will be made there. Nested containers provide isolation boundaries where registrations can be specialized (overriden).

constructor

Container's constructor takes an optional argument, next, which indicates where the container is nested. Containers form a chain back to the root container, so if next is omitted, it will automatically be chained to root.

behavior

can - property - determines if the container can resolve an item with all dependencies.

get - function - gets an item according to its registration.

has - function - determines if an item has a registration.

register - function - registers a named item and returns its Registration.

fulfill - function - registers a callback function to be invoked when the function's arguments can be fulfilled.

when - function - registers a callback invoked when an item can be resolved with all dependencies.

Registration

Registrations are used to instruct the container as to how each registered object should be treated. It establishes the behavior related to each.

Function Descriptions

get: gets an item registered with the container

var it =minioc.get('what');

If you're getting something that takes arguments, you may fulfill those arguments, overriding any injected or configured values.

var it =minioc.get('what',{ domain:'joe.bob.me'});

has: determines if the container can fulfill requests for an item

if(minioc.has('what')){

console.log('yay');

}

register: registers an item with the container.

// as a bare-value...

minioc.register('calculator').as.value(newCalculator);

// ... or as a factory ...

minioc.register('calculator').as.factory(function(){

returnnew(Calculator());

});

// ... or as a ctor ...

minioc.register('calculator').as.ctor(Calculator);

fulfill: shedules a callback function to be invoked as soon as all of its dependencies can be met.

// an identifying name must be provided with a fulfillment callback;

// this example indicates it is dependent on `config`, so minioc will

// invoke the callback as soon as it can inject the `config`

// dependency...

minioc.fulfill('must-be-named',function($config){

console.log('Yay! We have a `config` '+ $config);

});

when: provides a callback to be invoked when a particular, named item gets registered.

minioc.when('calculator',function(it){

// `it` refers to the calculator here.

};

Usage

require minioc:

var minioc =require('minioc');

Obtaining a Container

Once imported, minioc can be used as the root container.

var minioc =require('minioc')

, expect =require('expect.js')

;

expect(minioc).to.have.property('can');

expect(minioc).to.have.property('has');

expect(minioc).to.have.property('get');

expect(minioc).to.have.property('when');

expect(minioc).to.have.property('register');

expect(minioc).to.have.property('fulfill');

### Registering Bald Values

Any javascript object can be placed in the container by name and retrieved later.