depin
v1.0.0
Published
A simple dependency injection container.
Downloads
2
Readme
Install
npm install --save depin
Quick start
const depin = require('depin');
const app = depin();
app.set('beatles', [
{ name: 'George' },
{ name: 'John' },
{ name: 'Paul' },
{ name: 'Ringo' },
]);
app.register('beatleService' (beatles) => ({
getCount() {
return beatles.length;
},
}));
app.get('beatleService').count(); // 4
API
apply( factory:function ) :*
Run a function with all arguments injected from the DI container by name and return the result.
app.set({ one: 1, two: 2, three: 3 });
app.apply((one, two, three) => one + two + three); // 6
get( [name:string] ) :*
Get a value from the DI container.
app.set({ one: 1 });
app.get('one'); // value
When no name is passed app.get
will return a dependency picker which is very useful when combined with object destructuring.
app.set({ one: 1, two: 2 });
const { one, two } = app.get();
register( factory:function ) :app
Register a factory.
app.set({ one: 1, two: 2 });
app.register(function myManager(one, two) {
return { add: () => one + two };
});
app.get('myManager').add(); // 3
Or set its name directly.
app.set({ one: 1, two: 2 });
app.register('myManager', (one, two) => {
return { add: () => one + two };
});
app.get('myManager').add(); // 3
run( func:function ) :app
Run a function with all arguments injected from the DI container by name.
app.set({ one: 1, two: 2 });
app.run((one, two) => {
//
});
set( properties:object ) :app
/ set( name:string, value:* ):app
Set values in the DI container.