Skip to content

Commit 16851bc

Browse files
author
Umed Khudoiberdiev
committed
implementing scoped services
1 parent 91e239e commit 16851bc

File tree

13 files changed

+338
-80
lines changed

13 files changed

+338
-80
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Service} from "../../src/decorators/Service";
2+
import {Factory} from "./Factory";
3+
import {FactoryToken} from "./FactoryToken";
4+
5+
@Service({ id: FactoryToken, scope: "prototype" })
6+
export class BeanFactory implements Factory {
7+
8+
create() {
9+
console.log("bean created");
10+
}
11+
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface Factory {
2+
3+
create(): any;
4+
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {Token} from "../../src/Token";
2+
import {Factory} from "./Factory";
3+
4+
export const FactoryToken = new Token<Factory>("factories");
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Service} from "../../src/decorators/Service";
2+
import {Factory} from "./Factory";
3+
import {FactoryToken} from "./FactoryToken";
4+
5+
@Service({ id: FactoryToken, scope: "prototype" })
6+
export class SugarFactory implements Factory {
7+
8+
create() {
9+
console.log("sugar created");
10+
}
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {Service} from "../../src/decorators/Service";
2+
import {Factory} from "./Factory";
3+
import {FactoryToken} from "./FactoryToken";
4+
5+
@Service({ id: FactoryToken, scope: "prototype" })
6+
export class WaterFactory implements Factory {
7+
8+
create() {
9+
console.log("water created");
10+
}
11+
12+
}

sample/sample12-array-values/app.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import "reflect-metadata";
2+
import {Container} from "../../src/index";
3+
import {FactoryToken} from "./FactoryToken";
4+
import {SugarFactory} from "./SugarFactory";
5+
import {WaterFactory} from "./WaterFactory";
6+
import {BeanFactory} from "./BeanFactory";
7+
8+
Container.import([
9+
BeanFactory,
10+
SugarFactory,
11+
WaterFactory,
12+
]);
13+
const factories = Container.getMany(FactoryToken);
14+
factories.forEach(factory => factory.create());
15+
console.log(factories);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Service} from "../../src/decorators/Service";
2+
import {QuestionRepository} from "./QuestionRepository";
3+
4+
@Service({ scope: "request" })
5+
export class QuestionController {
6+
7+
constructor(protected questionRepository: QuestionRepository) {
8+
}
9+
10+
save(name: string) {
11+
if (name)
12+
this.questionRepository.userName = name;
13+
this.questionRepository.save();
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Service} from "../../src/decorators/Service";
2+
3+
@Service({ scope: "request" })
4+
export class QuestionRepository {
5+
userName: string;
6+
7+
save() {
8+
console.log(`saving question. author is ${this.userName}`);
9+
}
10+
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import "reflect-metadata";
2+
import {Container} from "../../src/index";
3+
import {QuestionRepository} from "./QuestionRepository";
4+
import {QuestionController} from "./QuestionController";
5+
6+
Container.import([
7+
QuestionController,
8+
QuestionRepository,
9+
]);
10+
const request1 = { param: "Timber" };
11+
const controller1 = Container.getFromRequest(request1, QuestionController);
12+
controller1.save("Timber");
13+
Container.removeFromRequest(request1, QuestionController);
14+
15+
const request2 = { param: "Guest" };
16+
const controller2 = Container.getFromRequest(request2, QuestionController);
17+
controller2.save("");
18+
19+
console.log((Container as any)["services"]);

0 commit comments

Comments
 (0)