component

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Invoke

func Invoke[T any](wrap *Wrap[T], opts ...dig.InvokeOption) error

Invoke 泛型函数,唤醒泛型类型的实例

func ResetDigContainer

func ResetDigContainer()

ResetDigContainer 允许重置并重建,非并发安全

Types

type DigContainer

type DigContainer struct {
	// contains filtered or unexported fields
}

DigContainer 封装dig容器,支持连缀操作,用于命令应用或web应用启动阶段实例化对象时解决依赖注入问题,不推荐web运行时使用。 // // e.g.

// 泛型实例包装器,实现了Set和Get方法 wrap := component.NewWrap[*OrderRepository]()

// 获取单例容器,连缀操作提供依赖构造器注册 dc := component.Container().

Provide(func() Xxx.IApplicationContext { return ctx }).  // 依赖ctx,匿名函数注入ctx依赖
Provide(model.NewOrderModel).
Provide(common-module.NewCommonModel)

// 验证依赖注入是否有错误

if dc.GetErrorCount() > 0 {
	return nil, fmt.Errorf("dig container init error: %v", dc.GetProvideErrs())
}

// 泛型函数唤醒泛型类型实例 err := component.Invoke[*OrderRepository](warp)

// 最终通过wrap的Get方法获取唤醒的实例 instance := wrap.Get()

// ==== or =================================================== // 非泛型方式,直接Invoke在回调函数中获取实例并执行处理逻辑

err := dc.Invoke(func(or *OrderRepository) error {
	err := or.DoSth()  // 执行处理逻辑
	return err
})

func Container

func Container() *DigContainer

Container 获取单例容器

func NewDigContainer

func NewDigContainer() *DigContainer

NewDigContainer 获取全新的依赖注入器

func NewDigContainerOnce

func NewDigContainerOnce() *DigContainer

NewDigContainerOnce 获取dig依赖注入器单例

func (*DigContainer) GetErrorCount

func (dc *DigContainer) GetErrorCount() int32

GetErrorCount 获取错误数

func (*DigContainer) GetProvideErrs

func (dc *DigContainer) GetProvideErrs() []error

GetProvideErrs 获取依赖注入错误

func (*DigContainer) Invoke

func (dc *DigContainer) Invoke(function interface{}, opts ...dig.InvokeOption) error

Invoke 唤醒目标类型的对象

func (*DigContainer) Provide

func (dc *DigContainer) Provide(constructor interface{}, opts ...dig.ProvideOption) *DigContainer

Provide 注入依赖,constructor为函数类型构造器,注入对象,使用函数封装返回对象

type Wrap

type Wrap[T any] struct {
	// contains filtered or unexported fields
}

func NewWrap

func NewWrap[T any]() *Wrap[T]

NewWrap 获取泛型包装器,用于Invoke唤醒泛型类型的实例

func (*Wrap[T]) Get

func (w *Wrap[T]) Get() T

func (*Wrap[T]) Set

func (w *Wrap[T]) Set(t T)

Directories

Path Synopsis
Package validate 提供基于 go-playground/validator 的多语言验证器包装器,支持自定义验证规则和错误消息翻译。
Package validate 提供基于 go-playground/validator 的多语言验证器包装器,支持自定义验证规则和错误消息翻译。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL