https://github.com/secjs/ioc
📦 Simple IoC Container for Node.js
https://github.com/secjs/ioc
container dependency-injection ioc nodejs
Last synced: 10 months 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 5 years ago)
- Default Branch: main
- Last Pushed: 2022-02-08T02:04:35.000Z (almost 4 years ago)
- Last Synced: 2025-01-28T10:25:15.768Z (12 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
[](https://github.com/jlenon7?tab=followers)
[](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:
