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.
@electron-tools/ioc
Advanced tools
A simple DI implement of IOC using typescript. inspired by vscode IOC implement.
# install by npm
$npm install @electron-tools/ioc
# install by pnpm
$pnpm add @electron-tools/ioc
# install by yarn
$yarn add @electron-tools/ioc
```~
# Usage
1. enable `experimentalDecorators` option in your tsconfig.json.
2. register your service by `service` decorator exported from this package.
```typescript
// src/serviceA.ts
import { service } from '@electron-tools/ioc';
@service('serviceA') // register ServiceA with unique id 'serviceA' using service decorator exported by this package.
class ServiceA {
// ....
}
inject
decorator inject your service to another service.// src/serviceB.ts
import { service, inject } from '@electron-tools/ioc';
@service('serviceB') // also register ServiceB with unique id 'serviceB'
class ServiceB {
constructor(
@inject('serviceA') // inject serviceA to serviceB, the only args passed to inject is the service unique id.
readonly serviceA: ServiceA,
) {}
}
// src/index.ts
import IOC from '@electron-tools/ioc';
import './serviceA.ts';
import './serviceB.ts';
const ioc = new IOC();
const serviceA = ioc.getService('serviceA');
const serviceB = ioc.getService('serviceB');
console.log(serviceA instanceof ServiceA); // true
console.log(serviceB instanceof ServiceB); // true
console.log(serviceA === serviceB.a); // true
ioc.getService('serviceA')
, serviceB will not be instance, cause serviceB is not dependencied by any service, but if you only call ioc.getService('serviceB')
, serviceA will be instance, and inject into serviceB. this maybe not what you want. you can init all services in one place by call ioc.init()
.const ioc = new IOC();
ioc.init(); // this statement will instance all services registered.
// src/serviceA.ts
import IOC, { service, inject } from '@electron-tools/ioc';
@service('serviceA') // register ServiceA with unique id 'serviceA' using service decorator exported by ioc.
class ServiceA {
constructor(
@inject('instantiationService') readonly ioc: IOC, // ioc itself is also a service can be injected.
) {}
someMethod() {
// dynamic get serviceB by ioc#getService API. then you can do anything what serviceB can do.
const serviceB = this.ioc.getService('serviceB');
serviceB.xxx;
}
}
FAQs
DI implement of IOC
The npm package @electron-tools/ioc receives a total of 0 weekly downloads. As such, @electron-tools/ioc popularity was classified as not popular.
We found that @electron-tools/ioc demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.