Skip to content

基于泛型实现的AOP库,简单轻量。A AOP library based on generic implementation, simple and lightweight.

License

Notifications You must be signed in to change notification settings

MicroWiller/AOP-golang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AOP-golang: A AOP library based on generic implementation.

🇬🇧 English | 🇨🇳 中文

Overview

AOP (Aspect Oriented Programming) is a kind of programming design idea, is the continuation of OOP (Object Oriented Programming), is a hot spot in software development. AOP can be used to isolate each part of business logic, so as to reduce the coupling degree between the parts of business logic, improve the reusability of the program, and improve the efficiency of development.

Installation

go get github.com/MicroWiller/AOP-golang

Usage

  1. First, define a structure type that implement the Aspect interface.
// BusProxy proxy bus
type BusProxy struct {
	bus *Bus
}

func (ap BusProxy) Pointcut(ctx context.Context) error {
	return ap.bus.Drive(ctx)
}

type Bus struct {
	Name       string
	Route      string
	Passengers int64
}
  1. Instantiate the generic architecture AOP.
// NewBus instantiate generic AOP for BusProxy.
func NewBus(name, route string, p int64) AOP[BusProxy] {
	busProxyAop := AOP[BusProxy]{}
	bus := Bus{
		Name:       name,
		Route:      route,
		Passengers: p,
	}
	proxy := BusProxy{bus: &bus}
	busProxyAop.SetProxy(proxy)
	return busProxyAop
}
  1. Instantiate a pre/post pointcut.
before := RegisterBefore[BusProxy]
aftere := RegisterAfter[BusProxy]
  1. Pointcut structures implement generic interfaces Before[T Aspect] / After[T Aspect].
type Police struct {
}

func (p Police) Before(ctx context.Context, bp *BusProxy) error {
    return nil
}

func (p Police) After(ctx context.Context, bp *BusProxy) {
}
  1. Generate Option load points.
p := Police{}
pBeforeOpt := before(p)
pAfterOpt := aftere(p)
  1. Execute AOP proxy methods.
busAop.Proxy(ctx, pBeforeOpt, pAfterOpt)

Finally

See more examples test file,Welcome to issue.

About

基于泛型实现的AOP库,简单轻量。A AOP library based on generic implementation, simple and lightweight.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages