Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/secjs/ioc
📦 Simple IoC Container for Node.js
https://github.com/secjs/ioc
container dependency-injection ioc nodejs
Last synced: about 1 month ago
JSON representation
📦 Simple IoC Container for Node.js
- Host: GitHub
- URL: https://github.com/secjs/ioc
- Owner: SecJS
- License: mit
- Created: 2020-07-18T16:31:28.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-08T02:04:35.000Z (almost 3 years ago)
- Last Synced: 2024-05-20T19:24:52.177Z (7 months ago)
- Topics: container, dependency-injection, ioc, nodejs
- Language: TypeScript
- Homepage:
- Size: 372 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# IoC Container 📦
> Very simple IoC container for NodeJS
[![GitHub followers](https://img.shields.io/github/followers/jlenon7.svg?style=social&label=Follow&maxAge=2592000)](https://github.com/jlenon7?tab=followers)
[![GitHub stars](https://img.shields.io/github/stars/secjs/ioc.svg?style=social&label=Star&maxAge=2592000)](https://github.com/secjs/ioc/stargazers/)
The intention behind this repository is to always maintain a viable and simple IoC to use in any type of `NodeJS` applications, with or without `Frameworks`
## Installation
```bash
npm install @secjs/ioc
```## Usage
### Set
> Container can register dependencies for you, every container.get will create a new instance for UserService.
```js
import { Container } from '@SecJS/IoC'
import { UserService } from 'app/Services/UserService'const container = new Container()
container.set(UserService)
const userService = container.get('UserService', 'props', 'to', 'UserService Constructor here')console.log(userService.findAll())
```### Singleton
> Container can register singleton dependencies, when singleton container.get will return the same UserService instance all time.
```js
import { Container } from '@SecJS/IoC'
import { UserService } from 'app/Services/UserService'const container = new Container()
container.singleton(UserService, 'props', 'to', 'UserService Constructor here')
const userService = container.get('UserService')console.log(userService.findAll())
```> Singleton can register static objects and arrays too, but for arrays and objects the name is required:
```js
container.singleton([], 'myArray')
container.singleton({}, 'myObject')console.log(container.get('myArray')) // []
console.log(container.get('myObject')) // {}
```---
## License
Made with 🖤 by [jlenon7](https://github.com/jlenon7) :wave: