前言
如果是做web开发,对依赖注入肯定不陌生,java程序员早就习惯了spring提供的依赖注入,做业务开发时非常方便,只关注业务逻辑即可,对象之间的依赖关系都交给框架。
golang是强类型语言,编译后是机器码,所以一般使用 反射 或 代码生成 解决依赖注入的问题
基于反射的DI
基于反射解决DI问题的框架, 使用比较多的是Uber的 dig 库
官方的例子:
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
|
type Config struct { Prefix string } //初始化Config函数 func NewConfig()(*Config, error ) { // In a real program, the configuration will probably be read from a // file. var cfg Config err := json.Unmarshal([] byte (`{ "prefix" : "[foo] " }`), &cfg) return &cfg, err } //初始化logger函数 func NewLogger(cfg *Config) *log.Logger { return log. New (os.Stdout, cfg.Prefix, 0 ) } func Handle() (l *log.Logger) { l. Print ( "You've been invoked" ) } func main() { //初始化dig对象 c := dig. New () //Provide方法用来设置依赖的对象 er := c.Provide(NewConfig) if err != nil { panic (err) } //设置依赖的对象 err = c.Provide(NewLogger) if err != nil { panic (err) } //执行Handle()方法 //Handle依赖 Config 和 Logger,使用Invoke执行方法时会自动注入依赖(依赖的对象要传入Provide方法中) err = c.Invoke(Handle) if err != nil { panic (err) } // Output: // [foo] You've been invoked } |
dig提供了一个容器(container),所有的依赖项通过Provide方法添加,执行某个方法时使用Invoke方法,该方法会自动注入所需要的依赖。
dig使用反射机制解决DI问题,所以代码执行性能上会有损耗
并且因为使用反射所以可能出现编译时没有错误,执行时报空指针
详情使用方法可以参考官方文档,dig可以继承到gin框架中,有兴趣的可以看看资料。
笔者不太喜欢这种使用方式,为了依赖注入破坏了代码原有的调用方式。
基于代码生成的DI
wire库是google出的解决golang DI问题的工具,它可以 自动生成依赖注入的代码,节省了手动去处理依赖关系
wire对原有代码的侵入度很低,开发过程中,在依赖注入代码处调用Build方法(例子中是初始化controller对象)就可以了
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
|
// +build wireinject package main import ( "encoding/json" "fmt" "github.com/google/wire" "net/http" ) type DataSource struct { Operation string } func NewDataSource() DataSource { return DataSource{Operation: "operation_name" } } //================== type Dao struct { DataSource DataSource } func NewDao(ds DataSource) *Dao { return &Dao{ DataSource: ds, } } func (d *Dao) GetItemList() ([] string , error ) { //TODO 拿到DB对象做查询操作 fmt.Printf( "db object: %s" , d.DataSource.Operation) return [] string {d.DataSource.Operation, "item1" , "item2" }, nil } //==================== type Service struct { Dao *Dao } func NewService(dao *Dao) *Service { return &Service{Dao: dao} } func (s *Service) GetItemList() ([] string , error ) { return s.Dao.GetItemList() } //===================== type Controller struct { Service *Service } func NewController(service *Service) *Controller { return &Controller{Service: service} } func (c *Controller) GetItemList() ([] string , error ) { return c.Service.GetItemList() } var MegaSet = wire.NewSet(NewDataSource, NewDao, NewService, NewController) func initializeController() *Controller { wire.Build(MegaSet) return &Controller{} } func getItemList(w http.ResponseWriter, r *http.Request) { controller := initializeController() itemList, _ := controller.GetItemList() output, _ := json.Marshal(itemList) fmt.Fprintf(w, string (output)) } func main() { http.HandleFunc( "/items" , getItemList) err := http.ListenAndServe( ":8080" , nil ) if err != nil { panic (err) } } |
然后再项目根目录执行wire命令,会生成构建好依赖关系的代码(以_gen结尾的文件)
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
|
// Code generated by Wire. DO NOT EDIT. //go:generate go run github.com/google/wire/cmd/wire //+build !wireinject package main import ( "encoding/json" "fmt" "github.com/google/wire" "net/http" ) // Injectors from main.go: // 此处是生成的代码 func initializeController() *Controller { dataSource := NewDataSource() dao := NewDao(dataSource) service := NewService(dao) controller := NewController(service) return controller } // main.go: type DataSource struct { Operation string } func NewDataSource() DataSource { return DataSource{Operation: "operation_name" } } type Dao struct { DataSource DataSource } func NewDao(ds DataSource) *Dao { return &Dao{ DataSource: ds, } } func (d *Dao) GetItemList() ([] string , error ) { fmt.Printf( "db object: %s" , d.DataSource.Operation) return [] string {d.DataSource.Operation, "item1" , "item2" }, nil } type Service struct { Dao *Dao } func NewService(dao *Dao) *Service { return &Service{Dao: dao} } func (s *Service) GetItemList() ([] string , error ) { return s.Dao.GetItemList() } type Controller struct { Service *Service } func NewController(service *Service) *Controller { return &Controller{Service: service} } func (c *Controller) GetItemList() ([] string , error ) { return c.Service.GetItemList() } var MegaSet = wire.NewSet(NewDataSource, NewDao, NewService, NewController) func getItemList(w http.ResponseWriter, r *http.Request) { controller := initializeController() itemList, _ := controller.GetItemList() output, _ := json.Marshal(itemList) fmt.Fprintf(w, string (output)) } func main() { http.HandleFunc( "/items" , getItemList) err := http.ListenAndServe( ":8080" , nil ) if err != nil { panic (err) } } |
关键代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//执行wire命令前的代码 func initializeController() *Controller { wire.Build(MegaSet) return &Controller{} } //执行后生成的代码 // Injectors from main.go: func initializeController() *Controller { dataSource := NewDataSource() dao := NewDao(dataSource) service := NewService(dao) controller := NewController(service) return controller } |
通过生成代码解决依赖注入的问题,既能提升开发效率,又不影响代码性能,wire更高级的用法可以去github document查看
-
tips: 如果报错误
other declaration of xxxx
,请在源文件头加上//+build wireinject
- go-zero框架也是用wire解决DI问题
到此这篇关于浅析golang的依赖注入的文章就介绍到这了,更多相关go依赖注入内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/bobo_simpler/article/details/125363060