快速了解 GO之接口解耦

更多个人笔记见:
github个人笔记仓库
gitee 个人笔记仓库

个人学习,学习过程中还会不断补充~ (后续会更新在github上)

文章目录

接口解耦的作用是便于切换三方库(项目需要或者三方库废弃不维护)等时候,不用大量修改代码而构建的设计

例子分析解耦

xorm 和 gorm 如果希望互相切换,使用上区别在于二者创建数据库的方法不同,xorm 为 Insert,gorm 为 Create

一般的构建思路

构建 xorm 的:

GO 复制代码
type XormDB struct{
	db *xorm.Session
	...	
}

type Trade struct {
	*XormDB
	...
}

func (t *Trade) InsertTrade( data interface{})  {
	t.db.Insert(data) 
	...
}

如果现在需要构建 gorm 的,就需要所有的替换成下面这样,同时接口等也都需要改变

GO 复制代码
type GormDB struct{
	db *Gorm.Session
	...	
}

type Trade struct {
	*GormDB
	...
}

func (t *Trade) InsertTrade( data interface{}) error  {
	t.db.Create(data) 
	...
}
解耦的构建思路
  • 所以采用接口的方法:
GO 复制代码
//初始化 xorm
type DBer interface {
	Insert(ctx context.Context, instance interface{})  //定义统一的 insert方法
}

type XormDB struct {
	db *xorm.Session
}
func (xorm *XormDB) Insert (ctx contesxt.COntext,instance ... interfaceP{}){
	xorm.db.Context(ctx).Insert(instance)
}

//初始化 gorm
type GormDB struct {
	db *gorm.DB
}
func (gorm *GormDB) Insert(ctx context.Context,instance... interface{}){
	gorm.db.Context(ctx).Create(instance)
}

//实际业务结构体
type Trade struct {
	db DBer
}
//初始化对应的数据库
func (t *Trade) AddDB(db DBer){
	t.db = db
}
//只要完成了 insert 方法就是可以的
func (t *Trade) AddTrade(ctx context.Context,instance interface{}){
	t.db.Insert(ctx,instance)
}

这样只用自己定义满足 DBer 接口的结构体,加入新的三方库就都是可以的,因为都是统一调用 Insert 方法

  • 另外一个同样解耦构建的例子:
    从底向上实现
GO 复制代码
// 定义数据访问层接口,这是一个统一的接口
type Repository interface {
    Create(entity interface{}) error
    //下面几个方法如果添加那么也要给GormRepository和XormRepository补上对应的 方法
    //FindByID(id uint, out interface{}) error
    //Update(entity interface{}) error
    //Delete(entity interface{}) error
}

注意:一般这几个不会放在同一个文件或者层次中的,比如model 层或者 dao 层等等,会在实际项目中划分开

GO 复制代码
// GORM实现
type GormRepository struct {
    db *gorm.DB
}

func (r *GormRepository) Create(entity interface{}) error {
    return r.db.Create(entity).Error
}

// XORM实现
type XormRepository struct {
    engine *xorm.Engine
}

func (r *XormRepository) Create(entity interface{}) error {
    _, err := r.engine.Insert(entity)
    return err
}

具体业务逻辑:

Go 复制代码
// 业务层只依赖Repository接口
type UserService struct {
    repo Repository
}

func NewUserService(repo Repository) *UserService {
    return &UserService{repo: repo}    //初始化对应的实例
}

func (s *UserService) CreateUser(user *User) error {
    return s.repo.Create(user)  //调用接口对应的 Create 函数就可以了
}

初始化的时候决定具体的实现,使用自己定义的结构体,对应 gorm 的GormRepository还是对应 xorm 的XormRepository

所以除去结构体的修改和补充,其实只要在这个地方进行改动就可以了

GO 复制代码
//使用GORM
db := gorm.Open(...)
service := NewUserService(&GormRepository{db: db})

//使用XORM
engine, _ := xorm.NewEngine(...)
service := NewUserService(&XormRepository{engine: engine})

(补充)工厂模式切换:

go 复制代码
func NewRepository(dbType string, conn interface{}) (Repository, error) {
    switch dbType {
    case "gorm":
        return &GormRepository{db: conn.(*gorm.DB)}, nil
    case "xorm":
        return &XormRepository{engine: conn.(*xorm.Engine)}, nil
    default:
        return nil, errors.New("unsupported database type")
    }
}

解耦的好处:

  • 切换ORM(例子中是 ORM)只需修改初始化代码
  • 易于单元测试(mock Repository)
  • 不一定固定依赖某个三方库
相关推荐
菩提小狗17 分钟前
Sqlmap双击运行脚本,双击直接打开。
前端·笔记·安全·web安全
码农小韩19 分钟前
基于Linux的C++学习——动态数组容器vector
linux·c语言·开发语言·数据结构·c++·单片机·学习
木风小助理20 分钟前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
yyy(十一月限定版)29 分钟前
初始matlab
开发语言·matlab
LawrenceLan30 分钟前
Flutter 零基础入门(九):构造函数、命名构造函数与 this 关键字
开发语言·flutter·dart
listhi52030 分钟前
基于MATLAB的支持向量机(SVM)医学图像分割方法
开发语言·matlab
嵌入式-老费33 分钟前
外壳3D结构设计(学习的方法)
学习
韩师傅33 分钟前
前端开发消亡史:AI也无法掩盖没有设计创造力的真相
前端·人工智能·后端
hui函数35 分钟前
如何解决 pip install 编译报错 g++: command not found(缺少 C++ 编译器)问题
开发语言·c++·pip
代码游侠37 分钟前
应用——智能配电箱监控系统
linux·服务器·数据库·笔记·算法·sqlite