
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
Inversion of Controll container with friendly API.
npm install --save container-ioc
import { Container, Inject } from 'container-ioc';
let container = new Container();
@Injectable()
class A {}
@Injectable()
class B {}
@Injectable()
class C {
constructor(@Inject(B) public b: B) { // use @Inject() decorator to mark dependencies
}
}
let providers = [
A,
{ token: 'IB', useClass: B },
{ token: C, useClass: C },
{ token: 'UseValue', useValue: 'any-primitive-or-object'},
{
token: 'FromFactory',
useFactory: () => {
return 'something';
}
},
{
token: 'FromFactoryWithInjections',
useFactory: (value, b, c) => {
return `${value + b.constructor.name + c.constructor.name}`;
},
inject: ['UseValue', 'IB', C]
}
];
container.register(providers);
let a: A = container.resolve(A);
let b: B = container.resolve('IB');
let c: C = container.resolve(C);
let value: string = container.resolve('UseValue');
let fromFactory: string = container.resolve('FromFactory');
let fromFactoryWithInjections: string = container.resolve('FromFactoryWithInjections');
Using string literals for tokens can become a head ache, use Injection Token instread:
import { InjectionToken, Container } from 'container-ioc';
let container = new Container();
interface IFactory {
create(): any;
}
const TFactory = new InjectionToken<IFactory>('IFactory'); // T in TFactory stands for token
@Injectable()
class ConcreteFactory implements IFactory {}
container.register({ token: TFactory, useClass: ConcreteFactory });
let factory: IFactory = container.resolve(TFactory);
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';
@Injectable()
class A {}
let parentContainer = new Container();
let childContainer = parentContainer.createScope();
parentContainer.register({ token: 'IA', useClass: A });
childContainer.resolve('IA');
By default metadata is assigned to static properties. If you want to use Reflect API for annotation, you can implement IMetadataAnnotator interface with your implementation using Reflect API. Then plug it into AnnotatorProvider
import { AnnotatorProvider, IMetadataAnnotator, Container } from 'container-ioc';
class ReflectMetadataAnnotator implements IMetadataAnnotator {
// your implementation
}
AnnotatorProvider.set(new ReflectMetadataAnnotator());
let container = new Container();
...
Feel free to submit a bug or a feature request. Or pick an issue from here and leave a comment with your proposal.
see CONTRIBUTION.md
FAQs
Dependency Injection and Inversion of Control (IoC) container
The npm package container-ioc receives a total of 57 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.