1 | # di-corate
|
2 | Another 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
|
12 | Use the library to engage the Dependency Injection in your project.
|
13 | The library does not use the `reflect-metadata` package.
|
14 | #### Simple DI
|
15 |
|
16 | **service1.ts**
|
17 | ```javascript
|
18 | import { Injectable } from 'di-corate';
|
19 |
|
20 | @Injectable()
|
21 | export class Service1 {
|
22 | do() {}
|
23 | }
|
24 | ```
|
25 |
|
26 | **service2.ts**
|
27 | ```javascript
|
28 | import { Injectable } from 'di-corate';
|
29 |
|
30 | @Injectable()
|
31 | export class Service2 {
|
32 | run() {}
|
33 | }
|
34 | ```
|
35 |
|
36 | **component.ts**
|
37 | ```javascript
|
38 | import { Inject, PropInject } from 'di-corate';
|
39 |
|
40 | export 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
|
58 | import { Injectable } from 'di-corate';
|
59 |
|
60 | @Injectable()
|
61 | export class HttpClient {
|
62 | get(url: string) { return 'response'; }
|
63 | }
|
64 | ```
|
65 |
|
66 | **service.ts**
|
67 | ```javascript
|
68 | import { Injectable, Inject } from 'di-corate';
|
69 |
|
70 | @Injectable()
|
71 | export 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
|
83 | import { Inject } from 'di-corate';
|
84 |
|
85 | export 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
|
100 | export interface Service {
|
101 | run(): void;
|
102 | }
|
103 | ```
|
104 |
|
105 | **default-service.ts**
|
106 | ```javascript
|
107 | export class DefaultService implements Service {
|
108 | run() {
|
109 | console.log('Implementation');
|
110 | };
|
111 | }
|
112 | ```
|
113 |
|
114 | **component.ts**
|
115 | ```javascript
|
116 | import { Container, Inject } from 'di-corate';
|
117 | import { DefaultService } from 'default-service';
|
118 |
|
119 | Container.addSingletone('Service', DefaultService);
|
120 |
|
121 | export 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 |