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

Implement custom shutdown function #38

Closed suzuki-safie closed 11 months ago

suzuki-safie commented 1 year ago

When using third-party types like sql.DB or redis.Client, it is often necessary to create a new type to specify the Shutdown behavior.

type DBWithShutdown sql.DB

func (db *DBWithShutdown) Shutdown() error {
    return db.Close()
}

This not only requires defining an additional type but also requires additional type conversions.

My proposal is:

injector := do.New()
do.Provide(injector, func(i *do.Injector) {
    return sql.Open(...)
}, do.WithShutdownFunc(func (db *sql.DB) error {
    return db.Close()
}))

do.Invoke[*sql.DB](injector)
injector.Shutdown()
suzuki-safie commented 11 months ago

@samber: Would you have any thoughts?