Skip to content

Commit

Permalink
extract Resolver and Binder interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mylxsw committed Mar 20, 2021
1 parent 77d7062 commit 4c230b3
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 16 deletions.
22 changes: 8 additions & 14 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,10 @@ func New() Container {
objectSlices: make([]*Entity, 0),
}

cc.MustSingleton(func() Container {
return cc
})

cc.MustSingleton(func() context.Context {
return context.Background()
})
cc.MustSingleton(func() Container { return cc })
cc.MustSingleton(func() context.Context { return context.Background() })
cc.MustSingleton(func() Binder { return cc })
cc.MustSingleton(func() Resolver { return cc })

return cc
}
Expand All @@ -135,13 +132,10 @@ func NewWithContext(ctx context.Context) Container {
objectSlices: make([]*Entity, 0),
}

cc.MustSingleton(func() Container {
return cc
})

cc.MustSingleton(func() context.Context {
return ctx
})
cc.MustSingleton(func() Container { return cc })
cc.MustSingleton(func() context.Context { return ctx })
cc.MustSingleton(func() Binder { return cc })
cc.MustSingleton(func() Resolver { return cc })

return cc
}
Expand Down
65 changes: 63 additions & 2 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ type Container interface {
SingletonWithKeyOverride(key interface{}, initialize interface{}) error
MustSingletonWithKeyOverride(key interface{}, initialize interface{})

HasBoundValue(key string) bool
BindValue(key string, value interface{}) error
MustBindValue(key string, value interface{})
BindValueOverride(key string, value interface{}) error
MustBindValueOverride(key string, value interface{})

HasBound(key interface{}) bool
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
Expand All @@ -77,7 +75,70 @@ type Container interface {

Provider(initializes ...interface{}) (func() []*Entity, error)
ExtendFrom(parent Container)

Must(err error)
Keys() []interface{}
CanOverride(key interface{}) (bool, error)
HasBoundValue(key string) bool
HasBound(key interface{}) bool
}

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 Resolver interface {
Resolve(callback interface{}) error
MustResolve(callback interface{})
ResolveWithError(callback interface{}) error
CallWithProvider(callback interface{}, provider func() []*Entity) ([]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{}

Must(err error)
Keys() []interface{}
HasBoundValue(key string) bool
HasBound(key interface{}) bool
}

0 comments on commit 4c230b3

Please sign in to comment.