【go】依赖倒置demo

文章目录

  • 前言
    • [1 项目目录结构:](#1 项目目录结构:)
    • [2 初始化函数](#2 初始化函数)
    • [3 router](#3 router)
    • [4 api](#4 api)
    • [5 service](#5 service)
    • [6 dao](#6 dao)
    • [7 Reference](#7 Reference)

前言

为降低代码耦合性,采用依赖注入的设计模式。原始请求路径:router -> api -> service -> dao。请求的为实际方法,具有层层依赖的关系。现将方法抽象为接口,即a依赖b,但a不创建(或销毁)b,仅使用b,b的创建(或销毁)交给容器。

1 项目目录结构:

复制代码
├─ddd
│  ├─router.go
│  │ 
│  ├─api
│  │  └─api_abstract.go   // 抽象接口
│  │  └─api_dog.go   // dog实现接口
│  │ 
│  ├─service
│  │  └─srv_abstract.go 
│  │  └─srv_dog.go  
│  │  
│  ├─dao
│  │  └─dao_abstract.go  
│  │  └─dao_dog.go  
│  │  
│  ├─model
│  │  ├─dto
│  │  └─schema

2 初始化函数

go 复制代码
// 依赖注入
func InitDog() *DogApiImpl  {
	d := dao.NewDogDaoImpl(global.DB)
	s := service.NewDogSrvImpl(d)
	return api.NewDogApiImpl(s)
}

3 router

go 复制代码
func Router() *gin.Engine {
	dog := init.InitDog()
	r.GET("/info", dog.Info)
	...
}

4 api

api_abstract.go中为抽象接口:

go 复制代码
type DogAPIIface interface {
	Create(ctx *gin.Context)
	Update(ctx *gin.Context)
	Info(ctx *gin.Context)
	List(ctx *gin.Context)
}

api_dog.go中为api的实现方法:

go 复制代码
type DogApiImpl struct {
	srv service.DogSrvIface
}

var _ DogAPIIface = (*DogApiImpl)(nil)

func NewDogApiImpl(srv service.DogSrvIface) *DogApiImpl {
	return &DogApiImpl{
		srv: srv,
	}
}

func (da *DogApiImpl) Info(ctx *gin.Context) {
	var req dto.DogInfoReq
	if err := ctx.ShouldBindUri(&req); err != nil {
		return
	}

	res, err := da.srv.Info(ctx, req.ID)
	if err != nil {
		return
	}

	helper.Response.ResponseSuccessWithData(ctx, consts.Success, res)
}

// 其他待实现方法
func (da *DogApiImpl) Create(ctx *gin.Context) {
}

...

5 service

srv_abstract.go中为抽象接口:

go 复制代码
type DogSrvIface interface {
	Create(ctx *gin.Context, req *dto.DogCreateReq) error
	Update(ctx *gin.Context, req *dto.DogUpdateReq) error
	Info(ctx *gin.Context, req *dto.DogInfoReq) (*dto.DogInfoRes, error)
	List(ctx *gin.Context, req *dto.DogListReq) ([]*dto.DogInfoRes, int, error)
}

srv_dog.go中为service的实现方法:

go 复制代码
type DogSrvImpl struct {
	dao dao.DogDaoIface
}

var _ DogSrvIface = (*DogSrvImpl)(nil)

func NewDogSrvImpl(dao dao.DogDaoIface) *DogSrvImpl {
	return &DogSrvImpl{
		dao: dao,
	}
}

func (ds *DogSrvImpl) Info(ctx *gin.Context, req *dto.DogInfoReq) (*dto.DogInfoRes, error) {
	var data dto.DogInfoReq

	// 具体业务逻辑
	dog, err := ds.dao.FindByID(ctx, id)
	if err != nil {
		return nil, err
	}

	return &dog, err
}
...

6 dao

dao_abstract.go中为抽象接口:

go 复制代码
type DogDaoIface interface {
	Create(ctx *gin.Context, req *dto.DogCreateReq) error
	Update(ctx *gin.Context, req *dto.DogUpdateReq) error
	Info(ctx *gin.Context, req *dto.DogInfoReq) (*dto.DogInfoRes, error)
	List(ctx *gin.Context, req *dto.DogListReq) ([]*dto.DogInfoRes, int, error)
}

dao_dog.go中为dao的实现方法:

go 复制代码
type DogDaoImpl struct {
	db *gorm.DB
}

var _ dao.DogDaoIface = (*DogDaoImpl )(nil)

func NewDogDaoImpl(db gorm.DB) *DogDaoImpl{
	return &DogDaoImpl{
		db: &db,
	}
}


func (ds *DogSrvImpl) Info(ctx *gin.Context, req *dto.DogInfoReq) (*dto.DogInfoRes, error) {
	
	// 具体业务逻辑
	return nil, nil
}
...

7 Reference

https://blog.hackerpie.com/posts/testing/golang-write-testable-codes/
https://juejin.cn/post/7146852457774055437

相关推荐
用户6757049885027 分钟前
Go 语言中如何操作二维码?
后端
这里有鱼汤9 分钟前
想成为下一个吉姆·西蒙斯,这十种经典K线形态你一定要记住
后端·python
SimonKing14 分钟前
吊打面试官系列:BeanFactory和FactoryBean的区别
java·后端·面试
Scoful17 分钟前
快速用 uv 模拟发布一个 Python 依赖包到 TestPyPI 上,以及常用命令
开发语言·python·uv
江湖十年23 分钟前
一行命令统计代码行数
后端·go·命令行
天天摸鱼的java工程师27 分钟前
互联网行业能力解刨:从Java后端八年开发经验看
前端·后端·程序员
brzhang33 分钟前
Android 16 卫星连接 API 来了,带你写出「永不失联」的应用
前端·后端·架构
程序员爱钓鱼42 分钟前
Go并发模型与模式:context 上下文控制
后端·google·go
clock的时钟1 小时前
c++第七天--继承与派生
开发语言·c++
AI小智1 小时前
AI提效99.5%!英国政府联手 Gemini,破解城市规划审批困局
后端