Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
ioc-service-container
Advanced tools
This is a lightweight library for a service container written in TypeScript.
Install the dependency with npm install ioc-service-container
First set up an Enum for preventing typos or redefinition of service ids:
export enum ServiceId {
TestApi = 'TestApi',
TestService = 'TestService',
FooApi = 'FooApi',
}
According to this you have to pass a factory of your required services to the ioc container. So at the initial script of
your application you call a function named e.g. setupServiced
:
function setupServiced() {
ServiceContainer.set(ServiceId.TestApi, () => new CustomTestApi());
ServiceContainer.set(ServiceId.FooApi, () => new CustomFooApi());
ServiceContainer.set(ServiceId.TestService, () => new CustomTestService());
}
Now you have two options to inject the requested service. The first one is without the usage of TypeScript annotations. This can be used anywhere in your code:
import { ServiceId } from './ServiceId';
import { ServiceContainer } from 'ioc-service-container';
const testService = ServiceContainer.get<TestService>(ServiceId.TestApi);
const testApi = ServiceContainer.get<TestService>(ServiceId.TestService);
The second option is to use the @inject
decorator inside a class:
export class CustomTestService implements TestService {
@inject
private readonly testApi!: TestApi; // Important is the naming of the property, its mapped to the sericeId
@inject(ServiceId.FooApi)
private readonly nameThisHowYouWant!: FooApi // If you don't want to name your property like the service id, use this decorator
}
Your can see a demo in the ./example
folder. To run this type in npm run example
.
Structuring your code and avoiding implizit dependencies is two of the most effective ways to avoiding bugs, especially when code gets extended. To goal of Dependency Injection (DI) is to prevent structures like this:
class CustomService {
constructor() {
this.api = new CustomApi();
}
}
The CustomService
has an implizit dependency to the CustomApi
.
The goal of DI is to encapsulate the dependencies of a class. The CustomService should work without knowing which api it is using. Following structure should be created:
+----------+ +-------------------+
| | | |
| Consumer +--->+ interface Service |
| | | |
+----------+ +---------+---------+
^
|
|
+---------+-----------+ +----------------+
| | | |
| class CustomService +---->+ interface Api |
| implements Service | | |
| | +--------+-------+
+---------------------+ ^
|
|
+--------+--------+
| |
| class CustomApi |
| implements Api |
| |
+-----------------+
(Btw asciiflow.com is a great tool for creating small charts for e.g. Readme.md)
FAQs
Lightweight ioc service container
The npm package ioc-service-container receives a total of 331 weekly downloads. As such, ioc-service-container popularity was classified as not popular.
We found that ioc-service-container demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.