UNPKG

2.8 kBMarkdownView Raw
1# di-corate
2Another dependency injection implementation for Typescript using decorators.
3
4[![Build Status](https://travis-ci.com/apashkov-ext/di-corate.svg?branch=main)](https://travis-ci.com/apashkov-ext/di-corate)
5[![npm version](https://img.shields.io/npm/v/di-corate)](https://www.npmjs.com/package/di-corate)
6[![install size](https://packagephobia.now.sh/badge?p=di-corate)](https://packagephobia.now.sh/result?p=di-corate)
7[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/git/git-scm.com/blob/master/MIT-LICENSE.txt)
8
9## Installation
10`npm install di-corate`
11## Using
12Use the library to engage the Dependency Injection in your project.
13The library does not use the `reflect-metadata` package.
14#### Simple DI
15
16**service1.ts**
17```javascript
18import { Injectable } from 'di-corate';
19
20@Injectable()
21export class Service1 {
22 do() {}
23}
24```
25
26**service2.ts**
27```javascript
28import { Injectable } from 'di-corate';
29
30@Injectable()
31export class Service2 {
32 run() {}
33}
34```
35
36**component.ts**
37```javascript
38import { Inject, PropInject } from 'di-corate';
39
40export class Component {
41 @PropInject(Service1) private readonly srv1: Service1;
42
43 constructor(@Inject(Service2) private readonly srv2: Service2) {
44 srv2.do();
45 example();
46 }
47
48 private example() {
49 this.srv1.run();
50 }
51}
52```
53
54#### Dependency tree
55
56**http-client.ts**
57```javascript
58import { Injectable } from 'di-corate';
59
60@Injectable()
61export class HttpClient {
62 get(url: string) { return 'response'; }
63}
64```
65
66**service.ts**
67```javascript
68import { Injectable, Inject } from 'di-corate';
69
70@Injectable()
71export class Service {
72 constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
73
74 do() {
75 const resp = this.http.get('someUrl');
76 console.log(resp);
77 }
78}
79```
80
81**component.ts**
82```javascript
83import { Inject } from 'di-corate';
84
85export class Component {
86 constructor(@Inject(Service) private readonly srv: Service) {
87 example();
88 }
89
90 private example() {
91 this.srv.do();
92 }
93}
94```
95
96#### Container setup
97
98**service.ts**
99```javascript
100export interface Service {
101 run(): void;
102}
103```
104
105**default-service.ts**
106```javascript
107export class DefaultService implements Service {
108 run() {
109 console.log('Implementation');
110 };
111}
112```
113
114**component.ts**
115```javascript
116import { Container, Inject } from 'di-corate';
117import { DefaultService } from 'default-service';
118
119Container.addSingletone('Service', DefaultService);
120
121export class Component {
122 constructor(@Inject('Service') private readonly srv: Service) {
123 example();
124 }
125
126 private example() {
127 this.srv.run(); // Console: 'Implementation'
128 }
129}
130```
131
132## Roadmap
133- ~~tests~~
134- ~~singletone registration~~
135- scoped registration
\No newline at end of file