Bootstrap

Go实战全家桶之一:goconfig 通过依赖注入实现单例和泛型实现简单工厂模式、工业级巨匠

goconfig

goconfig: git clone https://gitee.com/ichub/go.git

godi

ichub/godi

单例

匿名组合basedto.BaseEntitySingle

多实例

匿名组合basedto.BaseEntity

定义实体

type SimpleEntity struct {
    basedto.BaseEntitySingle 

    // 以下是依赖注入的示例
    // 注入的对象可以是单例、绑定、新建、无注入
    //auto  通过LoadBean()方法注入
    MultiAuto *multi.MultiEntity `godi:"auto"`
    //bind  通过BindUp()方法注入
    MultiBind *multi.MultiEntity `godi:"bind"`
    //new   通过New()方法注入
    MultiNew *multi.MultiEntity `godi:"new"`
    //none  不注入
    MultiNone *multi.MultiEntity `godi:"none"`
    //single  简单实例化; 注入的对象为简单NEW
    MultiSingle  *multi.MultiEntity `godi:"single"`
    MultiBindNew *multi.MultiEntity `godi:"bind"`

    Id   int
    Name string
}


注入代码生成godi di all

自动注入

package single

import (
    "gitee.com/ichub/goconfig/common/base/baseiface"
    "gitee.com/ichub/goconfig/common/base/baseutils/goutils"
    "gitee.com/ichub/goconfig/common/basedi"
    "github.com/sirupsen/logrus"

    "gitee.com/ichub/godi/di/dimodel/multi"
)

/*
 * ********************************************************
 * godi工具生成代码,不建议直接修改!
 * ********************************************************
 */

/* ********************************************************
    @Title  文件名称: simple_entity_init.go
    @Desp   描述:    自动注册注入

    @Author 作者: [email protected] 时间(2024-07-10 09:40:11)
    @Update 作者: [email protected] 时间(2024-07-10 09:40:11)

* *********************************************************/

const singleNameSimpleEntity = "single.SimpleEntity"

// init register load
func init() {
    registerBeanSimpleEntity()
}

// register SimpleEntity
func registerBeanSimpleEntity() {
    basedi.RegisterLoadBean(singleNameSimpleEntity, LoadSimpleEntity)
}

func FindBeanSimpleEntity() *SimpleEntity {
    bean, ok := basedi.FindBean(singleNameSimpleEntity).(*SimpleEntity)
    if !ok {
       logrus.Errorf("FindBeanSimpleEntity: failed to cast bean to *SimpleEntity")
       return nil
    }
    return bean

}

func LoadSimpleEntity() baseiface.ISingleton {
    var s = &SimpleEntity{
       Name: "test",
    }
    InjectSimpleEntity(s)
    return s

}

func InjectSimpleEntity(s *SimpleEntity) {

    // 自动实例化
    s.MultiAuto = multi.FindBeanMultiEntity()

    // 实例注册函数
    s.Bind()
    // 实例查找函数
    if find := s.FindBinding("MultiBind"); find != nil {
       goutils.Debug("find binded LoadBeanMultiEntity!")
       // 类型断言执行load
       s.MultiBind = find().(*multi.MultiEntity)
    } else {
       // 未找到绑定,实例化
       s.MultiBind = &multi.MultiEntity{}
       goutils.Error("no binding LoadBeanMultiEntity!")
    }

    // 简单实例化
    s.MultiSingle = &multi.MultiEntity{}

    // 实例注册函数
    s.Bind()
    // 实例查找函数
    if find := s.FindBinding("MultiBindNew"); find != nil {
       goutils.Debug("find binded LoadBeanMultiEntity!")
       // 类型断言执行load
       s.MultiBindNew = find().(*multi.MultiEntity)
    } else {
       // 未找到绑定,实例化
       s.MultiBindNew = &multi.MultiEntity{}
       goutils.Error("no binding LoadBeanMultiEntity!")
    }

    // 实例化默认New
    s.MultiNew = multi.NewMultiEntity()
    goutils.Debug("inject")
}


创建单例模式godi

31528e7bea2744e4bd838fe9d3f7cf98.png

func FindBean(name string) baseiface.ISingleton {
    var beanInfo = container.FindBean(name)
    if beanInfo.bean.Single() {
       return beanInfo.Bean()
    }
    return beanInfo.CreateBean()
}
func (this *TestDiFactoySuite) Test011_findBean() {

    var bean = basedi.FindBean("single.SimpleEntity")
    logrus.Info(bean)
}

INFO[2024-07-710 10:46:21]D:/go-ichub/go/godi/di/di_factroy_test.go:139  {
     "MultiAuto": {
          "Id": 0,
          "Name": "multi"
     },
     "MultiBind": {
          "Id": 0,
          "Name": "multi"
     },
     "MultiNew": {
          "Id": 0,
          "Name": "multi"
     },
     "MultiNone": null,
     "MultiSingle": {
          "Id": 0,
          "Name": ""
     },
     "MultiBindNew": {
          "Id": 0,
          "Name": ""
     },
     "Id": 0,
     "Name": "test"

简单工厂模式

泛型接口,获取单例

func (this *TestDiFactoySuite) Test012_findBeanFactroyr() {

    var bean = basedi.FindBeanInst[*single.SimpleEntity]()
    logrus.Info(bean)
}
func FindBeanInst[T any]() T {

    var inst T
    var instType = reflect.TypeOf(inst)
    var intance = FindBean(instType.String()[1:])

    return intance.(T)
}

两次获取的地址相同,为同一个实例godi

func (this *TestDiFactoySuite) Test013_findBeanFactroyr() {

    var bean1 = basedi.FindBeanInst[*single.SimpleEntity]()
    var bean2 = basedi.FindBeanInst[*single.SimpleEntity]()

    logrus.Info(bean1, bean2)
}

bean1,bean2地址是相同的

4e829b2721b044cd8254e0548522efa9.png

;