
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
container-ioc
Advanced tools
is Dependency Injection and IoC container for Typescript projects (ES6+ API is coming). It manages the dependencies between classes, so that applications stay easy to change and maintain as they grow.
npm install --save container-ioc
import { Container, Inject, Injectable } from 'container-ioc';
let container = new Container();
@Injectable()
class App {}
interface IService {}
@Injectable()
class Service implements IService {
constructor(@Inject('IService') public service: IService) {}
}
let providers = [
{ token: App, useClass: App },
{ token: 'IService', useClass: Service }
];
container.register(providers);
let app = container.resolve(App);
interface IService {}
@Injectable()
class ConcreteService {}
container.register({ token: 'IService', useClass: ConcreteService });
container.resolve('IService');
interface IService {}
const TService = new InjectionToken<IService>('IService'); // T stands for Token, you can pick another prefix
@Injectable()
class ConcreteService {}
container.register({ token: TService, useClass: ConcreteService });
container.resolve(TService);
By default, resolved instances are singletons. You can change that by setting provider's attribute LifeTime to LifeTime.PerRequest.
import { Container, Injectable, LifeTime } from 'container-ioc';
const container = new Container();
@Injectable()
class A {}
container.register({ token: A, useClass: A, lifeTime: LifeTime.PerRequest });
const instance1 = container.resolve(A);
const instance2 = container.resolve(A);
// instance1 !== instance2
if a provider wasn't found in a container it will look up in ascendant containers if there are 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 10 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
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.