issues
search
hhstore
/
blog
My Tech Blog: about Mojo / Rust / Golang / Python / Kotlin / Flutter / VueJS / Blockchain etc.
https://github.com/hhstore/blog/issues
290
stars
24
forks
source link
Golang: Dependency Injection
#284
Open
hhstore
opened
3 years ago
hhstore
commented
3 years ago
related:
[x] xxx
[x] xxx
hhstore
commented
3 years ago
Go 的 依赖注入:
依赖注入:
https://en.wikipedia.org/wiki/Dependency_injection
Dependency injection: Java 社区比较流行的概念.
部分前端框架 Angular/React 之类的, 有些复杂难懂的库, 多半是因为引入了它.
坦白讲: 我也不喜欢这种东西. 凡晦涩难懂, 不符合直觉的东西, 都是不好的设计.
应用场景:
数据层:
外部依赖资源(mysql, redis, kafka)对象
, 与
业务单元
的解耦.
见过其他尝试在别的层次使用, 但是, 不觉得恰当. 故不提.
Go + Wire:
https://github.com/google/wire
https://github.com/uber-go/dig
https://github.com/uber-go/fx
https://github.com/facebookarchive/inject
实践案例:
web 框架:
https://github.com/go-kratos/kratos
项目生成器:
https://github.com/go-kratos/kratos-layout
电商示例:
https://github.com/go-kratos/beer-shop
Java + Spring:
后端实践:
https://dagger.dev/
https://wiki.jikexueyuan.com/project/spring/dependency-injection.html
JS + Angular:
https://angular.cn/guide/dependency-injection
https://sangka.github.io/react-in-patterns-cn/chapter-10/
前端实践
Dart + GetX:
[x]
https://github.com/jonataslaw/getx
个人最喜欢的路由框架, 支持依赖注入.
⭐⭐⭐⭐⭐
总体使用, 算是比较恰当.
https://github.com/jonataslaw/getx/blob/master/documentation/zh_CN/dependency_management.md
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/dependency_management.md
https://github.com/jonataslaw/getx#dependency-management
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/route_management.md
https://github.com/jonsamwell/flutter_simple_dependency_injection
https://github.com/google/inject.dart
https://github.com/dart-ecosystem/ioc.dart
https://medium.com/flutter-community/dependency-injection-in-flutter-f19fb66a0740
https://levelup.gitconnected.com/dependency-injection-in-dart-flutter-apps-3332f1a61041
个人对 wire 的看法:
[x] 不喜欢, 不讨厌.
[x] 不喜欢:
go 是简洁的东西, 而 wire 增加大量理解复杂度.
引入收益, 可能小于: 因复杂度带来的困扰, 以及因不理解滥用, 导致的副作用.
[x] 不讨厌:
理解依赖注入的应用场景, 确实是个解耦工具.
wire 自带 代码生成器, 凡是可以偷懒的, 我都不讨厌.
ref:
https://farer.org/2021/04/21/go-dependency-injection-wire/
⭐⭐⭐⭐⭐
记录本篇, 纯粹是这篇前同事的 blog 写的太好. 所以, 还是把 wire, 介绍一下.
对比糟糕的讨论:
https://www.zhihu.com/question/32108444
知乎这么多人, 没人用简洁的人话, 讲明白.
我太懒了, 懒得去写东西.
就看上面这篇前同事的 blog 吧. 看得懂, 就用, 看不懂, 就不要用. 2333
官方的教程:
https://github.com/google/wire/blob/main/docs/guide.md
hhstore
commented
3 years ago
1
hhstore
commented
3 years ago
1
hhstore
commented
3 years ago
1
related: