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

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

@kuangyunsheng

Description

@kuangyunsheng

总觉得每一个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,
	}
}

这样看起来干净一些

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions