pdi-js
A simple "promiseful" node.js DI:
- Write services as factory functions, one service per file
- Create a pdi-js instance by specifying the directory where your services are.
- Use the pdi-js to get a service instance (the result of a factory function) by specifying the path of your service (where the root implicitely is the directory we talked about previously)
- A pdi-js instance can be created with a "suffix" : only files with this suffix will be considered as service factory
- Once a pdi-js instance is created, anything can be added to it and later be retrieved as a service
Installation
$ npm install pdi-js --save
Use without suffix
When the service files are located in a dedicated directory, it's convenient to use pdi-js without suffix:
const PDI = ; /////////////////////// create a new di /////////////////////// // the argument used for construction points to your service directoryconst myServiceDirectory = __dirname + '/services';var pdi = myServiceDirectory; ///////////////////////////////// get a service from a file ///////////////////////////////// // will use the service file {your service directory}/bar.jspdi; // will use the service file {your service directory}/foo/bar.jspdi; // will use the files {your service directory}/bar.js and {your service directory}/foo/bar.jspdi; // will use the files {your service directory}/bar.js and {your service directory}/foo/bar.jspdi;
Use with a suffix
When the service files are mixed up with other files, just use a suffix, pdi-js will ignore all unsuffixed files.
const PDI = ; /////////////////////// create a new di /////////////////////// // Let's say your services are mixed up with other files.// You have to construct a pdi-js instance pointing to your souce directory.const mySourceDirectory = __dirname;// Now, we choose a suffix for our service filesconst mySuffix = '.srv';var pdi = mySourceDirectory mySuffix; ///////////////////////////////// get a service from a file ///////////////////////////////// // will use the service file {your source directory}/bar.srv.jspdi; // will use the service file {your source directory}/foo/bar.srv.jspdi; // will use the files {your source directory}/bar.srv.js and {your source directory}/foo/bar.srv.jspdi; // will use the files {your source directory}/bar.srv.js and {your source directory}/foo/bar.srv.jspdi;
Statically store a pdi-js instance
A PDI instance can also be "stored" then be used "statically":
const PDI = ;const pdi = `service/directory`;PDI; // Later, in some other file...const PDI = ;PDI; PDIclear; // the PDI instance is not stored anymore (useful for unit testing)
Service files are factory function
The services are node.js modules that simply returns a factory function :
// {your service directory}/random.jsconst random = { return Math;};moduleexports = random
// {your service directory}/foo.jsconst foo = { return 'foo';};foocache = true; // the result of this service factory function will be cached by PDImoduleexports = foo
Other services can be used as dependencies for your factory function:
// {your service directory}/baz.jsconst bar = { return 'bar';};bardependencies = 'foo' 'foo/bar';moduleexports = bar
// {your service directory}/baz.jsconst bar = { return 'bar';};bardependencies = foo: 'foo' fooBar: 'foo/bar';moduleexports = bar
Eventually, you can directly pass a service instance to the pdi, it will be "stored" as a cached service:
const someService = name: 'some service';pdi;pdi;
Use a composition
A composition is just a plain old javascript object describing a list of dependency free services. It's usefull when we want to initialize several services that will be later used as dependecies. A composition is usually used at the initialization time of an application, and get busy with stuff like db configuration/connection.
const composition = dbConnection: { const dbConnection = // do stuff here to get a db connection ; } flavour ;pdi ; // Now, the services "dbConnection" and "iceCreamFactory" can be used as dependencies or retrived by inversion of controlpdi ;