Skip to content
This repository has been archived by the owner on Nov 14, 2022. It is now read-only.

建议生成的dao代码更简洁一些 #128

Open
kuangyunsheng opened this issue Nov 2, 2021 · 0 comments
Open

建议生成的dao代码更简洁一些 #128

kuangyunsheng opened this issue Nov 2, 2021 · 0 comments

Comments

@kuangyunsheng
Copy link

kuangyunsheng commented Nov 2, 2021

总觉得每一个dao文件都包含重复的代码不太优雅,尽管是自动生成的。建议作如下简化:

定义一个baseDao:

type baseDao struct {
	Table string 
	Group string 
}

func (dao *baseDao) DB() gdb.DB {
	return g.DB(dao.Group)
}

func (dao *baseDao) Ctx(ctx context.Context) *gdb.Model {
	return dao.DB().Model(dao.Table).Safe().Ctx(ctx)
}
func (dao *baseDao) Transaction(ctx context.Context, f func(ctx context.Context, tx *gdb.TX) error) (err error) {
	return dao.Ctx(ctx).Transaction(ctx, f)
}

所有dao嵌入baseDao,例如:

type DemoDao struct {
        baseDao
	Columns DemoColumns 
}

type DemoColumns struct {
	Id       string
	Name     string 
	Description string
}

var demoColumns = DemoColumns{
	Id:       "id",
	Name:     "name",
	Description : "description ",
}

func NewDemoDao() *DemoDao {
	return &DemoDao{
		baseDao: baseDao{
			Group:   "default",
			Table:   "user",
		},
		Columns: demoColumns,
	}
}

这样看起来干净一些

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant