风格介绍
三层架构,全局变量型
对应传统的mvc架构,一般定义一个全局变量来操作数据库
代表beego、gin-vue-admin gf等
gin-vue-admin
不同于java的依赖注入,api,service,routine中每个包都有一个enter.go文件将包中的实例导出
│ enter.go
│
├─autocode
│ auto_code_example.go
│ enter.go
│
├─example
│ enter.go
│ exa_breakpoint_continue.go
│ exa_customer.go
│ exa_excel_parse.go
│ exa_file_upload_download.go
│
└─system
enter.go
jwt_black_list.go
sys_api.go
sys_authority.go
sys_autocode_history.go
sys_auto_code.go
sys_auto_code_interface.go
sys_auto_code_mysql.go
sys_auto_code_pgsql.go
sys_base_menu.go
sys_casbin.go
sys_dictionary.go
sys_dictionary_detail.go
sys_initdb.go
sys_initdb_mysql.go
sys_initdb_pgsql.go
sys_menu.go
sys_operation_record.go
sys_system.go
sys_user.go
type ServiceGroup struct {
SystemServiceGroup system.ServiceGroup
ExampleServiceGroup example.ServiceGroup
AutoCodeServiceGroup autocode.ServiceGroup
}
var ServiceGroupApp = new(ServiceGroup)
gva特点
1、核心两层,api和service,没有dao 使用一个文件将所有的包要导入的实例化对象集中导出
2、全局变量,使用全局db,数据库客户端来操作
gf
1、 controller service dao三层设计,dao层由框架生成。
2、controller service 文件导出本身实例
3、db实例由框架本身维护。
service
type (
// sUser is service struct of module User.
sUser struct{}
)
var (
// insUser is the instance of service User.
insUser = sUser{}
)
controller
var User = cUser{}
type cUser struct{}
func (c *cUser) SignUp(ctx context.Context, req *apiv1.UserSignUpReq) (res *apiv1.UserSignUpRes, err error) {
err = service.User().Create(ctx, model.UserCreateInput{
Passport: req.Passport,
Password: req.Password,
Nickname: req.Nickname,
})
return
}
func (c *cUser) SignIn(ctx context.Context, req *apiv1.UserSignInReq) (res *apiv1.UserSignInRes, err error) {
err = service.User().SignIn(ctx, model.UserSignInInput{
Passport: req.Passport,
Password: req.Password,
})
return
}