Package xdi
provides a centralized dependency injection manager which holds definitions for an application's objects/dependencies.
$ go get -u github.com/actforgood/xdi
Basic example:
// DiManager holds application's objects, dependencies.
// Do not inject it/use it directly, in your application's objects.
// It should be used only in the bootstrap process of your application and/or main.go,
// as a centralized container of dependencies.
// Note: instead of declaring a variable, you can also use the singleton provided by xdi.ManagerInstance().
var DiManager = xdi.NewManager()
func init() {
DiManager.AddDefinition(xdi.Definition{
ID: "app.repository.product",
Initializer: func() interface{} {
return NewDummyProductRepository()
},
Shared: true,
})
}
func init() {
DiManager.AddDefinition(xdi.Definition{
ID: "app.service.product",
Initializer: func() interface{} {
return NewDummyProductService(
DiManager.Get("app.repository.product").(ProductRepository),
)
},
Shared: true,
})
}
func main() {
productService := DiManager.Get("app.service.product").(ProductService)
isAvailable, _ := productService.CheckAvailability("some-sku", 2)
fmt.Println("isAvailable:", isAvailable)
}
Feel free to use this pkg if you like it and fits your needs.
As it is a light/lite pkg, you can also just copy-paste the code, instead of importing it, keeping the license header.
This package is released under a MIT license. See LICENSE.