dix
dix is golang simple dependency inject (di) kit lib
1.Dependencies can be injected through providers
.Implement dix.Provider interface
type XFieldProvider struct{}
func (XFieldProvider) Symbol() string {
return "xfp"
}
func (XFieldProvider) Provide(ctx context.Context, tag *Tag) (any, error) {
return "x_field", nil
}
.Using tags in the struct using values from the provider's Symbol method
type X struct {
Field1 string `dix:"from:xfp"`
Field2 string `dix:"from:xfp"`
}
.Bind registration provider and call DI
func main () {
dix.Logging(true)
dix.Binding[dix.Provider](XFieldProvider{})
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field1, x.Field2)
}
2.Support specifying a namespace, if not specified, use the default namespace
type X struct {
Field1 string `dix:"from:xfp;namespace:ns1"`
Field2 string `dix:"from:xfp;namespace:ns2"`
}
func main () {
dix.Logging(true)
dix.Binding[dix.Provider](XFieldProvider{}, "ns1", "ns2")
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field1, x.Field2)
}
3.Support binding interface implementation using namespace, The interface under the specified namespace will be injected into the implementation of the binding, The implementation of the interface will automatically inject (look at Article 5)
type StringerImpl struct{
}
func (StringerImpl) String() string {
return "string_impl"
}
type X struct {
Field1 fmt.Stringer `dix:"from:?;namespace:ns1"`
Field2 fmt.Stringer `dix:"from:?;namespace:ns2"`
}
func main () {
dix.Logging(true)
dix.Binding[fmt.Stringer](StringerImpl{}, "ns1", "ns2")
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field1, x.Field2)
}
4.Supports binding of specified type values using namespace, The type under the specified namespace will be injected into the values of the binding
type X struct {
Field1 string `dix:"from:?;namespace:ns1"`
Field2 int `dix:"from:?;namespace:ns1"`
}
func main () {
dix.Logging(true)
dix.Binding[string]("stringValue", "ns1")
dix.Binding[int](100, "ns1")
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field1, x.Field2)
}
5.Supports automatic injection of struct and pointer struct
type StringerImpl struct{
Field string `dix:"from:?"`
}
func (StringerImpl) String() string {
return "string_impl"
}
type X struct {
Field string `dix:"from:?"`
FieldS fmt.Stringer `dix:"from:?"`
FieldY Y `dix:"from:?"`
}
type Y struct {
Field string `dix:"from:?"`
}
func main () {
dix.Logging(true)
dix.Binding[string]("stringValue")
dix.Binding[fmt.Stringer](StringerImpl{})
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field, x.FieldS.Field, x.FieldY.Field)
}
6.Support cycled dependency injection check
type X struct {
Field string `dix:"from:?"`
FieldY Y `dix:"from:?"`
}
type Y struct {
Field string `dix:"from:?"`
FieldX X `dix:"from:?"`
}
func main () {
dix.Logging(true)
dix.Binding[string]("stringValue")
x, err := dix.DI[X](context.Background())
if err != nil {
}
}
7.Support provider's custom tags
type XFieldProvider struct{}
func (XFieldProvider) Symbol() string {
return "xfp"
}
func (XFieldProvider) Provide(ctx context.Context, tag *Tag) (any, error) {
tagValue, ok := tag.GetCustomize("kind")
if ok && tagValue == "kind01" {
return "kind01", nil
}
return "x_field", nil
}
type X struct {
Field1 string `dix:"from:xfp;kind:kind01"`
Field2 string `dix:"from:xfp;kind:kind02"`
}
func main () {
dix.Logging(true)
dix.Binding[dix.Provider](XFieldProvider{})
x, err := dix.DI[X](context.Background())
if err != nil {
}
fmt.Println(x.Field1, x.Field2)
}
8.When there is no binding value of the specified type, the type value can be instantiated, and instantiation of map, slice, chan, array, and other types of zero values can be supported
type X struct {
Field0 string `dix:"from:?"`
Field1 int `dix:"from:?"`
Field2 []string `dix:"from:?;slice_len:3;slice_cap=3"`
Field3 [5]string `dix:"from:?"`
Field4 map[string]string `dix:"from:?;map_size:3"`
Field5 chan string `dix:"from:?;chan_buf:3"`
}
func main () {
dix.Logging(true)
x, err := dix.DI[X](context.Background())
if err != nil {
}
}
9.Usage suggestions
.Using single instance injection mode
.Do not inject unnecessary fields
.Using Tree Hierarchy for Injection