
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
container-ioc
Advanced tools
Annotation based Inversion of Controll container implementation.
npm install --save container-ioc
Container's API and behaviour is very similar to the one used in Angular4.
import { Container, Inject } from 'container-ioc';
let container = new Container();
// Register classes:
class RandomClass1 {};
class RandomClass3 {
doSomething(): void {
console.log('hello world');
}
};
// Use Injector decorater to inject dependencies:
class RandomClass2 {
constructor(@Inject(RandomClass3) public instance3: any) {
}
};
// Register classes in the container:
// You can pass just a Class literal alone or a provider literal ->
// { token: 'string or class literal', useClass: 'class literal' }
let providers = [
RandomClass1,
{ token: 'anystring', useClass: RandomClass2 },
{ token: RandomClass3, useClass: RandomClass3 }
];
container.register(providers);
// Resolve instances
let instance1: RandomClass1 = container.resolve(RandomClass1);
let instance2: RandomClass2 = container.resolve('anystring');
instance2.instance3.doSomething(); // hello world
let Container = require('container-ioc').Container;
let container = new Container();
// Register classes:
class RandomClass1 {};
class RandomClass3 {
doSomething() {
console.log('hello world');
}
};
// Use Injector decorater to inject dependencies:
class RandomClass2 {
instance3;
constructor() {
this.instance3 = container.resolve(RandomClass3);
}
};
// Register classes in the container:
// You can pass just a Class literal alone or a provider literal ->
// { token: 'string or class literal', useClass: 'class literal' }
let providers = [
RandomClass1,
{ token: 'anystring', useClass: RandomClass2 },
{ token: RandomClass3, useClass: RandomClass3 }
];
container.register(providers);
// Resolve instances
let instance1 = container.resolve(RandomClass1);
let instance2 = container.resolve('anystring');
instance2.instance3.doSomething(); // hello world
if a provider wasn't found in a container it will look up in ascendant containers if there's any:
import { Container } from 'container-ioc';
class SomeClass {
doStuff(): void {
console.log('hello world');
}
}
let parentContainer = new Container();
let childContainer = parentContainer.createScope();
parentContainer.register({ token: 'ISome', useClass: SomeClass });
childContainer.resolve('ISome');
let instance = childContainer.resolve('ISome');
instance.doStuff(); // hello world
It's just a very basic version. I'm going to add more functionality to meet common use cases, such as ability to mark injections in constructor with types only (typescript projects) and more handy API.
If you want to help feel free to submit an bug or a feature request. Also leave a comment under the issue you'd like to work on.
see CONTRIBUTION.md
FAQs
Dependency Injection and Inversion of Control (IoC) container
The npm package container-ioc receives a total of 62 weekly downloads. As such, container-ioc popularity was classified as not popular.
We found that container-ioc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.