-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
115 lines (100 loc) · 2 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const sdijs = require("./index.js");
const $Inject = new sdijs({
verbose: true
});
// ======================================================
// Config Object
const int = 10;
const CONFIG = {
int,
url: 'foo',
db: {
name: 'dev',
url: 'http://127.0.0.1:1234'
}
}
// utils.js function
const utils = ({config}) => {
return {
test: () => {
return config.int * config.int;
}
};
}
//Database.js Class
class Database {
constructor({'config': configAlias, utils}) {
this.config = configAlias;
this.utils = utils;
}
query(type) {
return this.config.db;
}
}
//HttpClient.js Class
class HttpClient {
constructor({config, utils}) {
this.config = config;
this.utils = utils;
}
getInfo() {
return 'get ' + this.config.url;
}
}
//Repository.js Class
class Repository {
constructor({database}) {
this.database = database;
}
getUser() {
return this.database.query('user');
}
}
//Service.js Class
class Service {
constructor({repository, httpClient}) {
this.repository = repository;
this.httpClient = httpClient;
}
getAll(filter) {
return {
filter,
users: this.repository.getUser(),
info: this.httpClient.getInfo()
}
}
}
//Controller.js Class
class Controller {
constructor({service}) {
this.service = service;
}
home(filter) {
return this.service.getAll(filter);
}
}
//App.js Class
class App {
constructor({controller, config, utils}) {
this.config = config;
this.controller = controller;
this.utils = utils;
}
router(filter) {
return {
home: this.controller.home(filter)
};
}
}
$Inject.addSingleton(CONFIG, 'config');
$Inject.addSingleton(utils, 'utils');
$Inject.addSingleton(HttpClient);
$Inject.addSingleton(Database);
$Inject.addSingleton(Repository);
$Inject.addSingleton(Service);
$Inject.addSingleton(Controller);
// RESOLVE
$Inject.addSingleton(App);
const app = $Inject.resolve('app');
console.log(app.router('foo'));
console.log(app.controller.home('bar'));