快速了解 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)
  • 不一定固定依赖某个三方库
相关推荐
晨曦5432104 分钟前
针对医学大数据的 Python 爬虫实践指南
开发语言·爬虫·python
小白学大数据7 分钟前
Python爬虫:多线程环境下503错误的并发控制优化
开发语言·爬虫·python
moxiaoran575317 分钟前
uni-app项目实战笔记17--获取系统信息getSystemInfo状态栏和胶囊按钮
笔记·uni-app
智者知已应修善业30 分钟前
【51单片机2位数码管100毫秒的9.9秒表】2022-5-16
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
卖猪肉的痴汉37 分钟前
5.2 Qt Creator 使用FFmpeg库
开发语言·qt·ffmpeg
知青春之宝贵38 分钟前
BEV感知-课程学习详细记录(自动驾驶之心课程)
学习
HeartException41 分钟前
Spring Boot + MyBatis Plus + SpringAI + Vue 毕设项目开发全解析(源码)
人工智能·spring boot·学习
饕餮争锋1 小时前
设计模式笔记_创建型_单例模式
java·笔记·设计模式
teeeeeeemo1 小时前
Number.toFixed() 与 Math.round() 深度对比解析
开发语言·前端·javascript·笔记
why1511 小时前
6.15 操作系统面试题 锁 内存管理
后端·性能优化