A promise-enabled dependency-injection module designed for Synth, but can be used for your own projects.
npm install synth-di
var DI = ;// Create a new dependency systemvar di = ;
A service is a function that can be requested by another service.
They're just ordinary JS functions, except:
A and Bdepend on
Cis resolved once and returned to both A and B.
Let's say you're writing an Express server and want to fetch some data for an admin user, and then send a response. It needs two things: The user (guaranteed to be an admin), and a connection to the DB.
var DI = ;var di = ;// service name specified as first parameterdi;// The order you register services doesn't matterdi;var dbConnection = processenvMONGO_DB;// service name extracted from given named-functiondi;// Now that we have our services registered, let's get that admin user and data!app;
Registers a service/function that can be used by another service/function.
The service name can be specified as the first parameter, or as the name of the function passed in.
Executes the specified service. The requested service should already be registered before executing it.
It returns a promise that then returns the result of the call.
An optional 2nd parameter can be provided to provide service dependencies at execution time (overriding already registered services).
The keys are the names of the services, and the values are passed in as those services, untouched.
Unlike a registered service, if a value is a function, it will NOT be invoked first, the function itself will be passed in.