![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
totally-di
Advanced tools
Simple dependency injection container for Node, forked from di-namic. ECMAScript 6 only.
To facilitate better the design and testing of modules, by decoupling dependencies via the Inversion of Control principle.
Simply put: Don't instantiate dependencies in the module!
The following examples are of modules that have dependencies on other modules.
Prototype-based (not recommended (ES5 Classes); with promises)
module.exports = TestObj1;
function TestObj1(dependency1, dependency2) {
this.__dependency1 = dependency1;
this.__dependency2 = dependency2;
}
TestObj1.prototype.testMethod = function () {
const self = this;
return new Promise((resolve, reject) => {
self.__dependency1.testMethod()
.then(result1 => {
self.__dependency2.testMethod(result1)
.then(result2 => {
resolve(result2);
})
.catch(err => {
return reject(err);
});
})
.catch(err => {
reject(err);
});
});
};
OR
Class-based (recommended (ES6 Classes))
module.exports = class TestObj1 {
constructor(dependency1, dependency2) {
this.__dependency1 = dependency1;
this.__dependency2 = dependency2;
}
async testMethod() {
try {
const result1 = await this.__dependency1.testMethod();
return await this.__dependency2.testMethod(result1);
} catch (err) {
throw err;
}
}
};
The module is therefore not responsible for creating instances of a dependency, but is instead relying on a consumer to do the instantiation for us. From a testing perspective, this approach allows dependencies to be externally mocked.
register
Register a dependency. Each time resolve
is called, a new instance of the dependency is returned.
container.register(alias, dependency, ctorArgAliases)
registerFactory
Register a dependency. Each time resolve
is called, a new instance of the dependency is returned, using the dependency's own factory method.
container.registerFactory(alias, dependency, factoryMethodName)
registerSingleton
Register a dependency. Each time resolve
is called, a singleton instance of the dependency is returned. The container maintains a single instance.
container.registerSingleton(alias, dependency)
registerSingletonFactory
Register a dependency. Each time resolve
is called, a singleton instance of the dependency is returned. The container maintains a single instance, which is originally created by the dependency's own factory method.
container.registerSingletonFactory(alias, dependency, factoryMethodName)
resolve
Returns an instance of a dependency.
container.resolve(alias)
Using the framework requires an understanding of the principles of dependency injection, particularly constructor injection.
Javascript modules would typically rely on external dependencies introduced via the constructor function.
When using di-namic in a Node application:
register
(with variations)resolve
The examples below are based on the module sample above.
TestObj1
↳ Dependency1
↳ Dependency2
index.js
const Container = require('di-namic').ContainerV2;
const Dependency1 = require('../lib/dependency1');
const Dependency2 = require('../lib/dependency2');
const TestObj1 = require('../lib/testObj1');
const app = require('../app');
module.exports = class Index {
constructor() {
this.__container = new Container();
}
//register the dependencies with the container
async register() {
await this.__container.register('Bob', Dependency1);
await this.__container.register('Mary', Dependency2);
// 'Joe' relies on 'Bob' and 'Mary'
await this.__container.register('Joe', TestObj1, ['Bob', 'Mary']);
}
async initialize() {
await this.register();
// resolve the dependency (including sub-dependencies)
var testObj1 = await this.__container.resolve('Joe');
// now start the app with dependencies injected into the constructor
await app.start(testObj1);
}
};
The above example is rather contrived - see the tests for a more comprehensive dependency tree example.
Please credit the author where appropriate. License below.
MIT License
Copyright (c) 2017
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Simple dependency injection container forked from di-namic for Node
We found that totally-di demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.