Documentation
¶
Overview ¶
Package container 实现了依赖注入容器,用于管理Golang对象的创建。
c := container.New()
c.BindValue("conn_str", "root:root@/my_db?charset=utf8")
c.Singleton(func(c container.Container) (*UserRepo, error) {
connStr, err := c.Get("conn_str")
if err != nil {
return nil, err
}
return &UserRepo{connStr: connStr.(string)}, nil
})
c.Prototype(func(userRepo *UserRepo) *UserService {
return &UserService{repo: userRepo}
})
if err := c.Resolve(func(userService *UserService) {
if userService.GetUser() != expectedValue {
t.Error("test failed")
}
}); err != nil {
t.Errorf("test failed: %s", err)
return
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Binder ¶
type Binder interface {
Prototype(initialize interface{}) error
MustPrototype(initialize interface{})
PrototypeWithKey(key interface{}, initialize interface{}) error
MustPrototypeWithKey(key interface{}, initialize interface{})
PrototypeOverride(initialize interface{}) error
MustPrototypeOverride(initialize interface{})
PrototypeWithKeyOverride(key interface{}, initialize interface{}) error
MustPrototypeWithKeyOverride(key interface{}, initialize interface{})
Singleton(initialize interface{}) error
MustSingleton(initialize interface{})
SingletonWithKey(key interface{}, initialize interface{}) error
MustSingletonWithKey(key interface{}, initialize interface{})
SingletonOverride(initialize interface{}) error
MustSingletonOverride(initialize interface{})
SingletonWithKeyOverride(key interface{}, initialize interface{}) error
MustSingletonWithKeyOverride(key interface{}, initialize interface{})
BindValue(key string, value interface{}) error
MustBindValue(key string, value interface{})
BindValueOverride(key string, value interface{}) error
MustBindValueOverride(key string, value interface{})
Bind(initialize interface{}, prototype bool, override bool) error
MustBind(initialize interface{}, prototype bool, override bool)
BindWithKey(key interface{}, initialize interface{}, prototype bool, override bool) error
MustBindWithKey(key interface{}, initialize interface{}, prototype bool, override bool)
Must(err error)
Keys() []interface{}
CanOverride(key interface{}) (bool, error)
HasBoundValue(key string) bool
HasBound(key interface{}) bool
}
type Conditional ¶
type Conditional interface {
// contains filtered or unexported methods
}
func WithCondition ¶
func WithCondition(init interface{}, onCondition interface{}) Conditional
WithCondition 创建 Conditional 接口实例 init 参数为传递个 Singleton/Prototype 方法的实例创建方法 onCondition 参数支持两种形式
- `onCondition(依赖注入参数列表...) bool`
- `onCondition(依赖注入参数列表...) (bool, error)`
type Container ¶
type Container interface {
Prototype(initialize interface{}) error
MustPrototype(initialize interface{})
PrototypeWithKey(key interface{}, initialize interface{}) error
MustPrototypeWithKey(key interface{}, initialize interface{})
PrototypeOverride(initialize interface{}) error
MustPrototypeOverride(initialize interface{})
PrototypeWithKeyOverride(key interface{}, initialize interface{}) error
MustPrototypeWithKeyOverride(key interface{}, initialize interface{})
Singleton(initialize interface{}) error
MustSingleton(initialize interface{})
SingletonWithKey(key interface{}, initialize interface{}) error
MustSingletonWithKey(key interface{}, initialize interface{})
SingletonOverride(initialize interface{}) error
MustSingletonOverride(initialize interface{})
SingletonWithKeyOverride(key interface{}, initialize interface{}) error
MustSingletonWithKeyOverride(key interface{}, initialize interface{})
BindValue(key string, value interface{}) error
MustBindValue(key string, value interface{})
BindValueOverride(key string, value interface{}) error
MustBindValueOverride(key string, value interface{})
Bind(initialize interface{}, prototype bool, override bool) error
MustBind(initialize interface{}, prototype bool, override bool)
BindWithKey(key interface{}, initialize interface{}, prototype bool, override bool) error
MustBindWithKey(key interface{}, initialize interface{}, prototype bool, override bool)
Resolve(callback interface{}) error
MustResolve(callback interface{})
ResolveWithError(callback interface{}) error
CallWithProvider(callback interface{}, provider EntitiesProvider) ([]interface{}, error)
Call(callback interface{}) ([]interface{}, error)
// AutoWire 自动对结构体对象进行依赖注入,object 必须是结构体对象的指针
// 自动注入字段(公开和私有均支持)需要添加 `autowire` tag,支持以下两种
// - autowire:"@" 根据字段的类型来注入
// - autowire:"自定义key" 根据自定义的key来注入(查找名为 key 的绑定)
AutoWire(object interface{}) error
MustAutoWire(object interface{})
Get(key interface{}) (interface{}, error)
MustGet(key interface{}) interface{}
Provider(initializes ...interface{}) EntitiesProvider
ExtendFrom(parent Container)
Must(err error)
Keys() []interface{}
CanOverride(key interface{}) (bool, error)
HasBoundValue(key string) bool
HasBound(key interface{}) bool
}
func Extend ¶
Extend create a new container, and it's parent is supplied container If can not found a binding from current container, it will search from parents
func NewWithContext ¶
NewWithContext create a new container with context support
type EntitiesProvider ¶
type EntitiesProvider func() []*Entity
type Entity ¶
type Entity struct {
// contains filtered or unexported fields
}
Entity represent a entity in container
func (*Entity) Value ¶
func (e *Entity) Value(provider EntitiesProvider) (interface{}, error)
Value instance value if not initialized
type Resolver ¶
type Resolver interface {
Resolve(callback interface{}) error
MustResolve(callback interface{})
ResolveWithError(callback interface{}) error
CallWithProvider(callback interface{}, provider EntitiesProvider) ([]interface{}, error)
Provider(initializes ...interface{}) EntitiesProvider
Call(callback interface{}) ([]interface{}, error)
// AutoWire 自动对结构体对象进行依赖注入,object 必须是结构体对象的指针
// 自动注入字段(公开和私有均支持)需要添加 `autowire` tag,支持以下两种
// - autowire:"@" 根据字段的类型来注入
// - autowire:"自定义key" 根据自定义的key来注入(查找名为 key 的绑定)
AutoWire(object interface{}) error
MustAutoWire(object interface{})
Get(key interface{}) (interface{}, error)
MustGet(key interface{}) interface{}
Must(err error)
Keys() []interface{}
HasBoundValue(key string) bool
HasBound(key interface{}) bool
}
Click to show internal directories.
Click to hide internal directories.