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]AOP Pointcut into interface method #142

Open
zhanjunjie2019 opened this issue Oct 19, 2022 · 1 comment
Open

[Proposal]AOP Pointcut into interface method #142

zhanjunjie2019 opened this issue Oct 19, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@zhanjunjie2019
Copy link

zhanjunjie2019 commented Oct 19, 2022

awesomeProject/ctx/ctx.go

  package ctx
  
  import "context"
  
  type AOPHandler func(*AOPContext) error
  
  type AOPContext struct {
	  context.Context
	  args []any
  }
  
  func (c *AOPContext) GetArg(i int) any {
	  return c.args[i]
  }
  
  func (c *AOPContext) Next() error {
           // TODO
	  return nil
  }

awesomeProject/aop/advice.go

  package aop
  
  import (
	  "awesomeProject/ctx"
	  "fmt"
  )
  
  // +ioc:autowire=true
  // +ioc:autowire:type=singleton
  
  type Advice1 struct {
  }
  
  func (a *Advice1) Before() ctx.AOPHandler {
	  return func(context *ctx.AOPContext) error {
		  fmt.Printf("Advice1 Before:func(%d)\n", context.GetArg(0))
		  return context.Next()
	  }
  }
  
  func (a *Advice1) After() ctx.AOPHandler {
	  return func(context *ctx.AOPContext) error {
		  fmt.Printf("Advice1 After:func(%d)\n", context.GetArg(0))
		  return context.Next()
	  }
  }

awesomeProject/service/service.go

  package service
  
  // +ioc:aop:before=Run-awesomeProject/aop.Advice1:Before
  // +ioc:aop:after=Run-awesomeProject/aop.Advice1:After
  
  type IService interface {
	  Run(int)
  }

awesomeProject/service/impl/service_impl.go

  package impl
  
  import "fmt"
  
  // +ioc:autowire=true
  // +ioc:autowire:type=allimpls
  // +ioc:autowire:implements=awesomeProject/service.IService
  
  type ServiceImpl1 struct {
  }
  
  func (s ServiceImpl1) Run(i int) {
	  fmt.Printf("this is ServiceImpl1 Run(%d)\n", i)
  }
  
  // +ioc:autowire=true
  // +ioc:autowire:type=allimpls
  // +ioc:autowire:implements=awesomeProject/service.IService
  
  type ServiceImpl2 struct {
  }
  
  func (s ServiceImpl2) Run(i int) {
	  fmt.Printf("this is ServiceImpl2 Run(%d)\n", i)
  }

awesomeProject/main.go

  package main
  
  import (
	  "awesomeProject/service"
	  "github.com/alibaba/ioc-golang"
  )
  
  func main() {
	  // start
	  if err := ioc.Load(); err != nil {
		  panic(err)
	  }
  
	  // app, err := GetAppIOCInterfaceSingleton is ok too
	  app, err := GetAppSingleton()
	  if err != nil {
		  panic(err)
	  }
	  app.Run()
  }
  
  // +ioc:autowire=true
  // +ioc:autowire:type=singleton
  
  type App struct {
	  Services []service.IService `allimpls:""`
  }
  
  func (a *App) Run() {
	  for i := range a.Services {
		  a.Services[i].Run(i + 100)
	  }
  }

Expected results:

  Advice1 Before:func(100)
  this is ServiceImpl1 Run(100)
  Advice1 After:func(100)
  Advice1 Before:func(101)
  this is ServiceImpl2 Run(101)
  Advice1 After:func(101)
@LaurenceLiZhixin LaurenceLiZhixin added the enhancement New feature or request label Oct 23, 2022
@LaurenceLiZhixin
Copy link
Collaborator

LaurenceLiZhixin commented Oct 23, 2022

Thanks, that's a good design, but interfece-based annotation now is not supported in our framework, which may causes some time to design, do you have any good idea? @zhanjunjie2019

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

No branches or pull requests

2 participants