更多个人笔记见:
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)
- 不一定固定依赖某个三方库