
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
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 A {}
class B {}
class C {
constructor(@Inject(B) public b: B) { // use @Inject() decorator to mark injections in a class
}
}
// 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 = [
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);
// Resolve instances
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
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';
class A {}
let parentContainer = new Container();
let childContainer = parentContainer.createScope();
parentContainer.register({ token: 'IA', useClass: A });
let a = childContainer.resolve('IA');
a.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 for typescript projects and more handy API.
If you want to help feel free to submit a 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 59 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.