前言

依赖注入的好处和特点这里不讲述了,本篇文章主要介绍gin框架如何实现依赖注入,将项目解耦。

项目结构


├── cmd 程序入口
├── common 通用模块代码
├── config 配置文件
├── controller API控制器
├── docs 数据库文件
├── models 数据表实体
├── page 页面数据返回实体
├── repository 数据访问层
├── router 路由
├── service 业务逻辑层
├── vue-admin Vue前端页面代码

相信很多Java或者.NET的码友对这个项目结构还是比较熟悉的,现在我们就用这个项目结构在gin框架中实现依赖注入。这里主要介绍controller、service和repository。

数据访问层repository

  1. 首先定义IStartRepo接口,里面包括Speak方法
//IStartRepo 定义IStartRepo接口
type IStartRepo interface {
Speak(message string) string
}
  1. 实现该接口,这里就不访问数据库了,直接返回数据
//StartRepo 注入数据库
type StartRepo struct {
Source datasource.IDb `inject:""`
} //Speak 实现Speak方法
func (s *StartRepo) Speak(message string) string {
//使用注入的IDb访问数据库
//s.Source.DB().Where("name = ?", "jinzhu").First(&user)
return fmt.Sprintf("[Repository] speak: %s", message)
}

这样我们就简单完成了数据库访问层的接口封装

业务逻辑层service

  1. 定义IStartService接口
//IStartService 定义IStartService接口
type IStartService interface {
Say(message string) string
}
  1. 实现IStartService接口方法,注入IStartRepo数据访问层
//StartService 注入IStartRepo
type StartService struct {
Repo repository.IStartRepo `inject:""`
} //Say 实现Say方法
func (s *StartService) Say(message string) string {
return s.Repo.Speak(message)
}

控制器controller

  1. 注入IStartService业务逻辑层,并调用Say方法
//Index 注入IStartService
type Index struct {
Service service.IStartService `inject:""`
} //GetName 调用IStartService的Say方法
func (i *Index) GetName(ctx *gin.Context) {
var message = ctx.Param("msg")
ctx.JSON(200, i.Service.Say(message))
}

注入gin中

到此三个层此代码已写好,然后我们使用"github.com/facebookgo/inject"Facebook的工具包将它们注入到gin中。

func Configure(app *gin.Engine) {
//controller declare
var index controller.Index
//inject declare
db := datasource.Db{}
//Injection
var injector inject.Graph
err := injector.Provide(
&inject.Object{Value: &index},
&inject.Object{Value: &db},
&inject.Object{Value: &repository.StartRepo{}},
&inject.Object{Value: &service.StartService{}},
)
if err != nil {
log.Fatal("inject fatal: ", err)
}
if err := injector.Populate(); err != nil {
log.Fatal("inject fatal: ", err)
} //database connect
err = db.Connect()
if err != nil {
log.Fatal("db fatal:", err)
}
v1 := app.Group("/")
{
v1.GET("/get/:msg", index.GetName)
}
}

有关更多的github.com/facebookgo/inject使用方法请参考文档

本demo源码地址:https://github.com/Bingjian-Zhu/gin-inject

最新文章

  1. Oracle 10g安装教程
  2. WINFORM时间控件(DATATIMEPICKER)的显示格式设置
  3. 转《WF编程》笔记目录
  4. ue4 模块的构建和加载
  5. SQL*Loader之CASE6
  6. Arcgis for Javascript 出现“init.js->TypeError: f is not a function”
  7. [Logstash]使用详解(转)
  8. C语言--指针问题_1
  9. elasticsearch介绍集群,模拟横向扩展节点、节点宕机、改变分片
  10. 关于使用Exception的一点心得
  11. JSON解析例子
  12. AsyncEnumerableExtensions.cs z
  13. Windows桌面快捷图标上的小箭头的恢复
  14. Hibernate 的原生 SQL 查询
  15. Ios App上传步骤
  16. Python--urllib3库详解1
  17. CentOS 7 使用yum安装出现错误
  18. python3 实例方法、类方法和静态方法
  19. TensorFlow与Flask结合识别手写体数字
  20. 《常见排序算法--PHP实现》

热门文章

  1. python笔记:配置虚拟开发环境
  2. 【2016NOI十连赛2-2】黑暗
  3. python-15-常用文件操作与注册登录练习
  4. Ansible-Tower自动化运维管理环境 - 安装破解记录
  5. GCC预编译宏查看
  6. 诚聘.NET架构师、高级开发工程师(2019年8月29日发布)
  7. centos 7 搭建Samba
  8. linux shell中$0,$?,$!等的特殊用法
  9. 反射实体类拼接SQL语句
  10. paypal开发指南