This container allows you to practice Dependency Injection with Functionnal Programming.
In the modern approach of javascript programming, you will find functionnal programming and pure functions as recommandations. This container is thought to allow those practices.
You can install this library with git clone.
yarn add fp-di-container
or
npm install fp-di-container --save
You can use the container this way:
const {createContainer} = require('fp-di-container')
const container = createContainer();
function func({ dependency }, param) {
console.log("dependency", dependency);
console.log("param", param);
}
container.register("func.service", func, { dependency: "gcp" });
const boundFunc = container.get("func.service");
boundFunc("fb");
Your index.js would look like that :
const express = require('express')
const server = express()
const port = 4001
const myContainer = require('./container')
server.get('/users', myContainer.get('usersController'))
server.listen(port, console.log(`server listenning on port ${port}`))
Your controller would have that kind of signature Notice that the returned signature is async function usersController (req, res)
async function usersController ({fetchUsers}, req, res) {
const users = await fetchUsers()
return res.send(users)
}
Your Business Logic would be like that
async function fetchUsers() {
return [
{name: "Bill"},
{name: "Steve"},
{name: "Mark"}
]
}
Your container usage would be like that
const {createContainer} = require('fp-di-container')
const container = createContainer()
// loading business logic functions
const fetchUsers = require('./fetchUsers')
// loading controllers
const usersController = require('./usersController')
// bind usefull functions on controllers
container.register('usersController', usersController, {fetchUsers})
module.exports = container;