Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rokkit-ts/rokkit.ts-dependency-injection
A Dependency Injection Library for the Rokkit.ts Framework based on TypeScript and Node.js
https://github.com/rokkit-ts/rokkit.ts-dependency-injection
backend dependency-injection framework microservice microservices nodejs rokkit typescript
Last synced: 16 days ago
JSON representation
A Dependency Injection Library for the Rokkit.ts Framework based on TypeScript and Node.js
- Host: GitHub
- URL: https://github.com/rokkit-ts/rokkit.ts-dependency-injection
- Owner: rokkit-ts
- License: mit
- Created: 2019-09-01T10:52:36.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T18:18:31.000Z (about 2 years ago)
- Last Synced: 2024-12-08T21:43:17.697Z (about 1 month ago)
- Topics: backend, dependency-injection, framework, microservice, microservices, nodejs, rokkit, typescript
- Language: TypeScript
- Homepage: https://rokkit.dev
- Size: 543 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# rokkit.ts-dependency-injection
[![Build Status](https://travis-ci.com/rokkit-ts/rokkit.ts-dependency-injection.svg?branch=master)](https://travis-ci.com/rokkit-ts/rokkit.ts-dependency-injection)
TypeScript dependency injection library using decorators for the rokkit.ts framework and other projects.
It provides a simple API to register classes and create instances of them.
The instances are handled in one container called `RokkitDI`.## Install and Build
To install the package:
```bash
npm install @rokkit.ts/dependency-injection
```## Usage
You can use two different ways of using the whole package.
The first way is to register your classes by decorators.
The second way is to use the native API of the RokkitDI container.### Using Decorators
The following example show a simple usage of the package.
The first listing shows the class that you want to be injected.```typescript
import { Injectable, Inject } from '@rokkit.ts/dependency-injection'@Injectable()
class DecoratedClass {
public foo: string
public bar: numberconstructor(@Inject('test') foo: string, @Inject(0.11) public bar: number) {
this.foo = foo
this.bar = bar
}
}
```After annotating the class, we can retrieve an singleton or an instance by the `RokkitDI`.
With this injector we could now create an instance of the corresponding class.```typescript
import { RokkitDI } from '@rokkit.ts/dependency-injection'
const instance: DecoratedClass = RokkitDI.singletonOf('DecoratedClass')console.log(`Foo: ${instance.foo}, Bar: ${instance.bar}`)
// Output: Foo: test, Bar: 0.11
```#### Automatic Constructor Argument scanning
The package provides the ability to scan classes arguments automatically. If any constructor argument is not decorated with the
@Inject
annotation, the base container tries to create automatically an instance for it.
(value:
any)#### Code Example
The first listing shows two classes the "FirstDecoratedClass" class has a dependency for the second class
"SecondDecoratedClass". Note that both class are annotated as@Injectable
, but only the second class
has@Inject
annotation present on the constructor. The package will now scan all user source code files for the constructor
argument and automatically adds the found arguments. This allows you to create an instance of "FirstDecoratedClass"
without annotating the constructor with a specific value for user object.By default the source code scan will look for the source code directory "./src". If you want to change this behavior
set the environment variableSRC_SCAN_DIR
to you preferable directory before starting your application.```typescript
import { Injectable, Inject } from '@rokkit.ts/dependency-injection'@Injectable()
class FirstDecoratedClass {
public classDependency: SecondDecoratedClassconstructor(classDependency: SecondDecoratedClass) {
this.classDependency = classDependency
}
}@Injectable()
class SecondDecoratedClass {
public foo: string
public bar: numberconstructor(@Inject('test') foo: string, @Inject(0.11) public bar: number) {
this.foo = foo
this.bar = bar
}
}
```### Use the native API
The native API let's you easy register an injectable class on the `RokkitDi` container.
In order to register a class you need to provide its arguments.```typescript
import { RokkitDI } from '@rokkit.ts/dependency-injection'class AClass {
constructor(public foo: string, bar: number)
}RokkitDI.registerInjectable(AClass, [
{ index: 0, type: 'string', value: 'test' },
{ index: 1, type: 'number', value: 0.11 }
])const instance: AClass = RokkitDI.singletonOf('AClass')
console.log(`Foo: ${instance.foo}, Bar: ${instance.bar}`)
// Output: Foo: test, Bar: 0.11
```## API Description
| Decorators | |
| :--------: | :--------------------------------------------- |
| Methods: |@Injectable(contextName?: string)
|
| |@Inject(value: any)
|| Class: | RokkitDI |
| :------: | :------------------------------------------ |
| Methods: |registerInjectable(Class)
|
| |singletonOf(className: string)
|
| |instanceOf(className: string)
|## Contribution
All kinds of contributions are welcome, no matter how big or small.
Before you start to contribute please read our [Code of Conduct](./CODE_OF_CONDUCT.md).In order to submit any contribution check out our [contribution guidelines](./CONTRIBUTION.md).
## License
Rokkit.ts-dependency-injection is Open Source software released under the MIT License.