当前位置: 首页 > article >正文

快速了解 GO之接口解耦

更多个人笔记见:
注意点击“继续”,而不是“发现新项目”
github个人笔记仓库 https://github.com/ZHLOVEYY/IT_note
gitee 个人笔记仓库 https://gitee.com/harryhack/it_note
个人学习,学习过程中还会不断补充~ (后续会更新在github上)

文章目录

    • 例子分析解耦
        • 一般的构建思路
        • 解耦的构建思路

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

例子分析解耦

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

一般的构建思路

构建 xorm 的:

type XormDB struct{db *xorm.Session...	
}type Trade struct {*XormDB...
}func (t *Trade) InsertTrade( data interface{})  {t.db.Insert(data) ...
}

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

type GormDB struct{db *Gorm.Session...	
}type Trade struct {*GormDB...
}func (t *Trade) InsertTrade( data interface{}) error  {t.db.Create(data) ...
}
解耦的构建思路
  • 所以采用接口的方法:

//初始化 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 方法

  • 另外一个同样解耦构建的例子:
    从底向上实现
// 定义数据访问层接口,这是一个统一的接口
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 层等等,会在实际项目中划分开

// 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
}

具体业务逻辑:

// 业务层只依赖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
所以除去结构体的修改和补充,其实只要在这个地方进行改动就可以了

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

(补充)工厂模式切换:

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

解耦的好处:

  • 切换ORM(例子中是 ORM)只需修改初始化代码
  • 易于单元测试(mock Repository)
  • 不一定固定依赖某个三方库
http://www.lryc.cn/news/2397149.html

相关文章:

  • 【多线程初阶】内存可见性问题 volatile
  • C++ 类模板三参数深度解析:从链表迭代器看类型推导与实例化(为什么迭代器类模版使用三参数?实例化又会是怎样?)
  • MySQL强化关键_018_MySQL 优化手段及性能分析工具
  • ASP.NET MVC添加模型示例
  • 【Part 3 Unity VR眼镜端播放器开发与优化】第二节|VR眼镜端的开发适配与交互设计
  • 第1天:认识RNN及RNN初步实验(预测下一个数字)
  • 全文索引详解及适用场景分析
  • 利用DeepSeek编写能在DuckDB中读PostgreSQL表的表函数
  • 树莓派安装openwrt搭建软路由(ImmortalWrt固件方案)
  • 排序算法——详解
  • Go整合Redis2.0发布订阅
  • 电子电气架构 --- 如何应对未来区域式电子电气(E/E)架构的挑战?
  • 鸿蒙OS基于UniApp的区块链钱包开发实践:打造支持鸿蒙生态的Web3应用#三方框架 #Uniapp
  • 易学探索助手-个人记录(十二)
  • Windows 账号管理与安全指南
  • Python窗体编程技术详解
  • 思维链提示:激发大语言模型推理能力的突破性方法
  • NVMe协议简介之AXI总线更新
  • 设计模式——责任链设计模式(行为型)
  • 基于Android的医院陪诊预约系统
  • 基于Spring Boot 电商书城平台系统设计与实现(源码+文档+部署讲解)
  • 【金融基础学习】债券回购方式
  • 第五十九节:性能优化-GPU加速 (CUDA 模块)
  • 单元测试-概述入门
  • ⚡ Hyperlane —— 比 Rocket 更快的 Rust Web 框架!
  • 《AI Agent项目开发实战》DeepSeek R1模型蒸馏入门实战
  • Ubuntu 24.04 LTS Chrome 中文输入法(搜狗等)失效?一行命令解决
  • 字节golang后端二面
  • 计算机网络物理层基础练习
  • vscode + cmake + ninja+ gcc 搭建MCU开发环境