di-injector helps you to manage dependency injection. It works in a very simple way: You just create a DiContainer and add all the dependencies you want to it, finally when you are coding your business logic, just mark the struct's fields you want to be injected with the tag: inject:"auto" and pass this structs to the library to manage the dependencies. The library will look for the "appropriate" dependency in its bag of dependencies and will inject the value.
package main
import di_injector "github.com/sebastianMurdoch/di-injector"
package main
import (
"fmt"
di_injector "github.com/sebastianMurdoch/di-injector"
)
func main() {
/* Create a container */
c := di_injector.NewDiContainer()
/* Add your dependencies */
c.AddToDependencies(" Of course")
c.AddToDependencies(&ServiceImpl{})
/* Inject your dependencies */
bo := businessObject{}
c.InjectWithDependencies(&bo)
fmt.Println(bo.SomeService.doService() + bo.CommonString)
}
type businessObject struct {
CommonString string `inject:"auto"`
SomeService Service `inject:"auto"`
}
type Service interface {
doService() string
}
type ServiceImpl struct {}
func (s *ServiceImpl) doService() string {
return "Can this library be more useless?"
}
To see a more usefull example please check out this post
Comments and PRs of any kind are welcome!! Thanks