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 |
|
6 | ## Installation
|
7 | `npm install composite-validation`
|
8 | ## Using
|
9 | Use the library to engage the Dependency Injection in your project.
|
10 | The library does not use the reflect-metadata package.
|
11 | #### Simple DI
|
12 |
|
13 | ```javascript
|
14 | //
|
15 | // service1.ts
|
16 | @Injectable()
|
17 | export class Service1 {
|
18 | do() {}
|
19 | }
|
20 | //
|
21 | // service2.ts
|
22 | @Injectable()
|
23 | export class Service2 {
|
24 | run() {}
|
25 | }
|
26 |
|
27 | //
|
28 | // component.ts
|
29 | export class Component {
|
30 | @PropInject(Service1) private readonly srv1: Service1;
|
31 |
|
32 | constructor(@Inject(Service2) private readonly srv2: Service2) {
|
33 | srv2.do();
|
34 | example();
|
35 | }
|
36 |
|
37 | private example() {
|
38 | this.srv1.run();
|
39 | }
|
40 | }
|
41 | ```
|
42 |
|
43 | #### Dependency tree
|
44 | ```javascript
|
45 | //
|
46 | // http-client.ts
|
47 | @Injectable()
|
48 | export class HttpClient {
|
49 | get(url: string) { return 'response'; }
|
50 | }
|
51 |
|
52 | //
|
53 | // service.ts
|
54 | @Injectable()
|
55 | export class Service {
|
56 | constructor(@Inject(HttpClient) private readonly http: HttpClient) { }
|
57 |
|
58 | do() {
|
59 | const resp = this.http.get('someUrl');
|
60 | console.log(resp);
|
61 | }
|
62 | }
|
63 |
|
64 | //
|
65 | // component.ts
|
66 | export class Component {
|
67 | constructor(@Inject(Service) private readonly srv: Service) {
|
68 | example();
|
69 | }
|
70 |
|
71 | private example() {
|
72 | this.srv.do();
|
73 | }
|
74 | }
|
75 | ```
|
76 | ## Roadmap
|
77 | - add tests |
\ | No newline at end of file |