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

[Proposal] Add aliases to the interface(Serivce) by adding annotations to simplify development #127

Open
photowey opened this issue Aug 23, 2022 · 3 comments

Comments

@photowey
Copy link
Contributor

At present, if you want to declare a specific implementation of an interface, you have to write a long fully qualified package name, which is very cumbersome. for example:

+ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service

// +ioc:autowire=true
// +ioc:autowire:type=singleton
// +ioc:autowire:implements=github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service

type serviceDefaultImpl struct {
}

func (s *serviceDefaultImpl) GetHelloString(name string) string {
	return fmt.Sprintf("This is serviceDefaultImpl, hello %s", name)
}

It is expected that aliases are added to interfaces through annotations, and then development is simplified by specifying aliases directly where needed.

Among them, it should be noted that:

  • Aliases are globally unique
  • When parsing aliases through CLI and then initiating alias registration, you need to pay attention to the timing of registration and use (sequence)

The effect achieved is as follows:

// +ioc:autowire:service:alias=HelloService
type Service interface {
	GetHelloString(string) string
}

// Notes:
// HelloService == github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service
// +ioc:autowire=true
// +ioc:autowire:type=singleton
// +ioc:autowire:implements=HelloService
type serviceDefaultImpl struct {
}

func (s *serviceDefaultImpl) GetHelloString(name string) string {
	return fmt.Sprintf("This is serviceDefaultImpl, hello %s", name)
}

// alias == "HelloService"
@LaurenceLiZhixin
Copy link
Collaborator

Thanks for your proposal @photowey , I have a question that, can we change ioc:autowire:service:alias to ioc:autowire:alias ? Like struct alias that already supported?

@photowey
Copy link
Contributor Author

I think this is achievable, however, there are a few things to keep in mind:

  • Are aliases for interfaces and aliases for instances allowed to be duplicated?

  • How to distinguish whether an alias is an interface alias or an instance alias?

Suggest:

When registering an interface(Service) alias, we can add a special token, such as: "&"

  // +ioc:autowire:alias=HelloService
  type Service interface {
  	GetHelloString(string) string
  }
  
  // HelloService == github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service
  
  // aliasMap
  // aliasMap["&HelloService"] = github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.Service
  
  // Notes:
  // KEYs starting with "&" default to the KEY of the interface alias
  // +ioc:autowire=true
  // +ioc:autowire:type=singleton
  // +ioc:autowire:alias=HelloService
  type ServiceImpl interface {
  
  }
  
  func(impl *ServiceImpl)GetHelloString(string) string {
      
  }
  
  // aliasMap
  // aliasMap["HelloService"] = github.com/alibaba/ioc-golang/example/autowire/autowire_active_profile_implements/service.ServiceImpl

@LaurenceLiZhixin
Copy link
Collaborator

I think it's ok to add a special token to interface alias @photowey

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

No branches or pull requests

2 participants