Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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/)


Buy Me A Coffee


GitHub language count

Repository size

License

Commitizen

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: