@dyrektrypt/container v2.3.2
container.js
Simple to learn and use, open-source project for emulating containers in Javascript using CommonJS.
See 'usage' for learning how to use the API.
See 'information' for more detailed information about the API.
Installation
Simply install the repository using npm.
#Assuming you are in your project's directory
npm i @dyrektrypt/container
And that's it! Now all you need to do is require it.
const container = require('@dyrektrypt/container')
Usage
The container emulates IoC by registering objects to it at runtime, which can simply be fetched by using the container.getObject(name) function. However the container also takes advantage of being weakly typed, allowing you to declare nodes with a name of type 'any' in which you can later retrieve.
Single Objects
To register a single object to the container, simply declare it as an export named 'node' with options:
- name: any (required)
- object: object (required)
- singleton: boolean (required)
An example of this:
exports.node = {
name: 123,
object: new testObject(),
singleton: true //It is singleton
}
Multiple Objects
To register multiple objects to the container, simply declare an export named 'node' in which exports an array of nodes. Just like a single node, it must contain objects with options:
- name: any (required)
- object: object (required)
- singleton: boolean (required)
An example of this:
let myObjects = [{
name: 123,
object: new testObject(),
singleton: false //It is not singleton
}]
exports.node = myObjects
And that's it! All objects will be individually registered to the container. Now all you need to do is fetch them from the container whenever you want.
container.getObject(123) //Fetches the testObject in either scenario
When a singleton is configured to equal false, a new object will be created upon every call.
Information
Due to the declarative approach of ES6, the container relies on CommonJS in order to dynamically run.
The container is good for emulating objects which have similiar functionality to statically typed languages such as module-wide IIFE's and classes.
The container makes sure that the objects logic is not touched by other ends of the program making it a good way to keep the program up-to-date and secure; as well as allowing for a singleton pattern for objects needed on a global level.
However it is not recommended that you just pass an object to the container for a singleton object, but instead you pass a new instance of one, for example:
exports.node = {
name: 'badPractice',
object: myBadPracticeObject, //This means that the objects logic can still be changed!
singleton: true
}
exports.node = {
name: 'goodPractice',
object: new myGoodPracticeObject(), //This objects logic won't be touched elsewhere!
singleton: true
}
This is because the 'badPractice' object can still be changed and updated elsewhere changing the logic of the emulated 'statically' typed object (which should not be done for most scenarios).
container.js is dependency-free.
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago