Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/small-tou/s-ioc
这是一个试验项目,主要是学习下如何实现 ioc 模式
https://github.com/small-tou/s-ioc
Last synced: 7 days ago
JSON representation
这是一个试验项目,主要是学习下如何实现 ioc 模式
- Host: GitHub
- URL: https://github.com/small-tou/s-ioc
- Owner: small-tou
- Created: 2021-12-24T09:34:28.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2021-12-27T00:58:29.000Z (almost 3 years ago)
- Last Synced: 2024-04-15T02:48:56.279Z (7 months ago)
- Language: TypeScript
- Size: 7.81 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
## 介绍
这是一个试验项目,主要是学习下如何实现 ioc 模式
```
npm run start
```## 使用
### 属性注入
```
@Injectable()
class User {
name = randomName(["tom", "jerry", "jack"]);
constructor() {}report() {
logger(colors().red().padding(3, 5).log(`i am ${this.name}`));
}
}@Injectable()
@Resolve()
class Dog {
name = randomName(["dog1", "dog2", "dog3"]);
@Inject(User)
public master?: User;
constructor() {}report() {
logger(colors().green().padding(3, 5).log(`this is dog ${this.name}`));
logger(colors().green().padding(3, 5).log(`dog's master is ${this.master?.name}`));
}
}@Resolve()
class Home {
name = randomName(["home1", "home2", "home3"]);
@Inject(User)
public user?: User;@Inject(Dog)
public dog?: Dog;
constructor() {
console.log("home created");
}report() {
logger(colors().blueBg().white().padding(3, 5).log("report home"));
logger(colors().blue().padding(3, 5).log(`this is home ${this.name}`));
logger(colors().redBg().white().padding(3, 5).log("report user"));
this.user?.report();
logger(colors().greenBg().white().padding(3, 5).log("report dog"));
this.dog?.report();
}
}
```### 构造参数注入
```
@Resolve()
class Home {
name = randomName(["home1", "home2", "home3"]);
constructor(@Inject(User) public user?: User, @Inject(Dog) public dog?: Dog) {
console.log("home created");
}report() {
logger(colors().blueBg().white().padding(3, 5).log("report home"));
logger(colors().blue().padding(3, 5).log(`this is home ${this.name}`));
logger(colors().redBg().white().padding(3, 5).log("report user"));
this.user?.report();
logger(colors().greenBg().white().padding(3, 5).log("report dog"));
this.dog?.report();
}
}
```## 待实现
- [x] 属性注入
- [x] 参数注入
- [] container 管理
- [] scope
- [] 依赖管理
- [] 循环依赖
- [] 注入参数
- [] ts 类型