samber / do

⚙️ A dependency injection toolkit based on Go 1.18+ Generics.
https://pkg.go.dev/github.com/samber/do
MIT License
1.71k stars 67 forks source link

v2: Feature Request - expose a method for add lifecycle hook #58

Closed kcmvp closed 2 months ago

kcmvp commented 4 months ago

right now life cycle hook can only be set when build a container as below `injector := do.NewWithOpts(&do.InjectorOpts{ HookAfterRegistration: func(injector do.Injector, serviceName string) { fmt.Printf("Service registered: %s\n", serviceName) }, HookAfterShutdown: func(injector do.Injector, serviceName string) { fmt.Printf("Service stopped: %s\n", serviceName) },

Logf: func(format string, args ...any) {
    log.Printf(format, args...)
},

})`

my case is that I am using do as DI container build some basic infra in the group for other projects. it's obvious that other teams(projects) also need hooks for project specific design. I export the DI but they can not register hook.

proposal: if HookAfterRegistration and HookAfterShutdown are slcie of functions and di expose a new method to add a new hook method then it will work in my case

of course there is gotcha: this new added method which for adding hook will throw error when the DI is not emtpy

thank you!

samber commented 2 months ago

I agree.

Let's change that before v2 release. A slice of hooks might be a breaking change in container options.

samber commented 2 months ago

Closing your ticket in favor of #70 + #71 + #72.