Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/genservice: It is recommended that the generated service supports multiple implementations #3735

Open
chehan01 opened this issue Aug 18, 2024 · 1 comment

Comments

@chehan01
Copy link

Description

在具体的业务实现中,有时需要对一个service去写多个不同的实现以应对不同的业务场景,目前GoFrame自动生成的service接口并不具备这个功能,我推荐自动生成的service可以参考以下格式:

package service

type IConfigService interface {
	Foo() string
}

var localConfigService = make(map[string]IConfigService)

func Config(configName ...string) IConfigService {
	if configName == nil {
		if localConfigService["default"] == nil {
			panic("implement not found for interface IConfigService, forgot register?")
		}
		return localConfigService["default"]
	}
	if len(configName) > 1 {
		panic("too many implement name!")
	}
	if localConfigService[configName[0]] == nil {
		panic("no such implement " + configName[0] + " for interface IConfigService, forgot register?")
	}
	return localConfigService[configName[0]]
}

func RegisterConfig(config IConfigService, name ...string) {
	if name == nil {
		localConfigService["default"] = config
	}
	for _, v := range name {
		localConfigService[v] = config
	}
}

这是我在实际业务中所用到的,如果以这种方式生成service层,那么在logic层注册和其他层调用时,既可以实现一个接口多个实现的调用,又可完美兼容原有的调用方式和逻辑。

望采纳。

Additional

No response

@Issues-translate-bot Issues-translate-bot changed the title cmd/genservice: 推荐生成的service支持多实现 cmd/genservice: It is recommended that the generated service supports multiple implementations Aug 18, 2024
@oldme-git
Copy link
Member

@chehan01
gf gen serrice is kind of recommended practice. If you needed special requirements, you could custom for service
gf gen service 只是一个推荐的作法,如果有特殊需求,可以自定义 service 层的实现

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

No branches or pull requests

2 participants