Documentation ¶
Index ¶
- Variables
- func Bind(bindingDef ...any) bool
- func Call(function any, parameters ...any) []any
- func ClearInstances()
- func Instance(instance any) bool
- func IsBound(binding any) bool
- func Make(abstract any, parameters ...any) any
- func MakeTo(makeTo any, parameters ...any)
- func Reset()
- func Singleton(singleton any, concreteResolverFunc ...any) bool
- func Tag(tag string, bindings ...any) bool
- func Tagged(tag string) []any
- type Binding
- type ContainerConfig
- type ContainerInstance
- func (container *ContainerInstance) Bind(bindingDef ...any) bool
- func (container *ContainerInstance) Call(function any, parameters ...any) []any
- func (container *ContainerInstance) ClearInstances()
- func (container *ContainerInstance) CreateChildContainer() *ContainerInstance
- func (container *ContainerInstance) Instance(instance any) bool
- func (container *ContainerInstance) IsBound(binding any) bool
- func (container *ContainerInstance) Make(abstract any, parameters ...any) any
- func (container *ContainerInstance) MakeTo(makeTo any, parameters ...any)
- func (container *ContainerInstance) ParentContainer() *ContainerInstance
- func (container *ContainerInstance) Reset()
- func (container *ContainerInstance) ResolveFunctionArgs(function reflect.Value, parameters ...any) []reflect.Value
- func (container *ContainerInstance) ResolveFunctionArgsWithInterceptor(function reflect.Value, interceptor FuncArgResolverInterceptor, ...) []reflect.Value
- func (container *ContainerInstance) Singleton(singleton any, concreteResolverFunc ...any) bool
- func (container *ContainerInstance) Tag(tag string, bindings ...any) bool
- func (container *ContainerInstance) Tagged(tag string) []any
- type FuncArgResolverInterceptor
- type Invocable
- func (invocable *Invocable) CallMethodByNameWith(methodName string, container *ContainerInstance, parameters ...any) []reflect.Value
- func (invocable *Invocable) CallMethodByNameWithArgInterceptor(methodName string, container *ContainerInstance, ...) []reflect.Value
- func (invocable *Invocable) CallMethodWith(container *ContainerInstance, parameters ...any) []reflect.Value
- func (invocable *Invocable) InstantiateStructAndFill(container *ContainerInstance) reflect.Value
- func (invocable *Invocable) InstantiateWith(container *ContainerInstance) any
- type PkgType
- type Types
Constants ¶
This section is empty.
Variables ¶
var Container = CreateContainer()
var ContainerTypes = &Types{}
Functions ¶
func ClearInstances ¶ added in v0.0.5
func ClearInstances()
Types ¶
type ContainerConfig ¶ added in v0.0.4
type ContainerConfig struct {
OnlyInjectStructFieldsWithInjectTag bool
}
ContainerConfig - Holds configuration values... soon I will add some more, make them work fully Right now this is a placeholder
type ContainerInstance ¶ added in v0.0.4
type ContainerInstance struct { Config *ContainerConfig // contains filtered or unexported fields }
ContainerInstance - Holds all of our container registration
func CreateChildContainer ¶ added in v0.0.5
func CreateChildContainer() *ContainerInstance
func CreateContainer ¶
func CreateContainer() *ContainerInstance
CreateContainer - Create a new container instance
func ParentContainer ¶ added in v0.0.5
func ParentContainer() *ContainerInstance
func (*ContainerInstance) Bind ¶ added in v0.0.4
func (container *ContainerInstance) Bind(bindingDef ...any) bool
Bind - Add a binding to the container, we can do this in a few different ways...
------
Function binding:
For example:
ContainerInstance.Bind(func() AbstractServiceInterface { return NewService() })
AbstractServiceInterface will be our resolver, NewService() is the bound type to resolve.
We can also just provide a concrete binding For example...
ContainerInstance.Bind(NewService)
------
Abstract Interface -> Concrete Implementation binding:
For example:
ContainerInstance.Bind((*AbstractServiceInterface)(nil), NewService)
Assuming the NewService func would return something like "*Service" This doesn't look great, but it's the only way I know of, to pass an interface as value
If "NewService" returns AbstractServiceInterface, you can just do
ContainerInstance.Bind(NewService)
------
Concrete binding:
For example:
ContainerInstance.Bind(SomeConcreteService{})
or
ContainerInstance.Bind(new(SomeConcreteService))
func (*ContainerInstance) Call ¶ added in v0.0.4
func (container *ContainerInstance) Call(function any, parameters ...any) []any
Call - Call the specified function via the container, you can add parameters to your function, and they will be resolved from the container, if they're registered
func (*ContainerInstance) ClearInstances ¶ added in v0.0.4
func (container *ContainerInstance) ClearInstances()
ClearInstances - This will just remove any singleton instances from the container When they are next resolved via Make/MakeTo, they will be instantiated again
func (*ContainerInstance) CreateChildContainer ¶ added in v0.0.4
func (container *ContainerInstance) CreateChildContainer() *ContainerInstance
CreateChildContainer - Returns a new container, any failed look-ups of our child container, will then be looked up in the parent, or returned nil
func (*ContainerInstance) Instance ¶ added in v0.0.4
func (container *ContainerInstance) Instance(instance any) bool
Instance - This is similar to Singleton, except with Singleton we provide a type to instantiate With instance, we provide an already instantiated value to the container
func (*ContainerInstance) IsBound ¶ added in v0.0.4
func (container *ContainerInstance) IsBound(binding any) bool
IsBound - Check if the provided value type exists in our container
func (*ContainerInstance) Make ¶ added in v0.0.4
func (container *ContainerInstance) Make(abstract any, parameters ...any) any
Make - Try to make a new instance of the provided value and return it This requires a type cast to work nicely... For example:
service := ContainerInstance.Make((*ServiceAbstract)(nil))
func (*ContainerInstance) MakeTo ¶ added in v0.0.4
func (container *ContainerInstance) MakeTo(makeTo any, parameters ...any)
MakeTo - Try to make a new instance of the provided value and assign it to your arg For example:
var service ServiceAbstract ContainerInstance.MakeTo(&service)
func (*ContainerInstance) ParentContainer ¶ added in v0.0.4
func (container *ContainerInstance) ParentContainer() *ContainerInstance
ParentContainer - Returns the parent container, if one exists
func (*ContainerInstance) Reset ¶ added in v0.0.4
func (container *ContainerInstance) Reset()
Reset - Reset will empty all bindings in this container, you will have to register any bindings again before you can resolve them.
func (*ContainerInstance) ResolveFunctionArgs ¶ added in v0.0.5
func (container *ContainerInstance) ResolveFunctionArgs(function reflect.Value, parameters ...any) []reflect.Value
ResolveFunctionArgs - Resolves the args of our function we bound to the container parameters is an array of values that we wish to provide which is optional parameters will first be assigned starting at index 0 of the functions args Then we'll look at the function args, and if we assigned a value from the parameters already it will use that, otherwise we'll look the type up in the container and resolve it
func (*ContainerInstance) ResolveFunctionArgsWithInterceptor ¶ added in v0.0.5
func (container *ContainerInstance) ResolveFunctionArgsWithInterceptor(function reflect.Value, interceptor FuncArgResolverInterceptor, parameters ...any) []reflect.Value
func (*ContainerInstance) Singleton ¶ added in v0.0.4
func (container *ContainerInstance) Singleton(singleton any, concreteResolverFunc ...any) bool
Singleton - Bind a "class" that should only be instantiated once when resolved in the future, the initial instantiation of this type will be returned
func (*ContainerInstance) Tag ¶ added in v0.0.4
func (container *ContainerInstance) Tag(tag string, bindings ...any) bool
Tag - When we've bound to the container, we can then tag the abstracts with a string This is useful when we want to obtain a "category" of implementations
For example; Imagine we have a few different "statistic gathering" services
// Bind our individual services Container.Bind(new(NewUserPostViewsStatService), func () {}) Container.Bind(new(NewPageViewsStatService), func () {}) // Add the services to the "StatServices" "Category" Container.Tag("StatServices", new(NewUserPostViewsStatService), new(NewPageViewsStatService)) // Now we can obtain them all Container.Tagged("StatServices")
func (*ContainerInstance) Tagged ¶ added in v0.0.4
func (container *ContainerInstance) Tagged(tag string) []any
Tagged - Resolve the instances from the container using the specified tag Refer to Tag to see how adding tagged bindings works
type FuncArgResolverInterceptor ¶ added in v0.0.5
type Invocable ¶ added in v0.0.3
type Invocable struct {
// contains filtered or unexported fields
}
func CreateInvocable ¶ added in v0.0.3
CreateInvocable - Binding should be a struct or function
func CreateInvocableFunction ¶ added in v0.0.3
CreateInvocableFunction - Pass a function reference through - skips the need to get/resolve the type etc
func CreateInvocableStruct ¶ added in v0.0.5
CreateInvocableStruct - Pass a struct reference through - skips the need to get/resolve the type etc
func (*Invocable) CallMethodByNameWith ¶ added in v0.0.3
func (invocable *Invocable) CallMethodByNameWith(methodName string, container *ContainerInstance, parameters ...any) []reflect.Value
CallMethodByNameWith - Call the method and assign its parameters from the passed parameters & container
func (*Invocable) CallMethodByNameWithArgInterceptor ¶ added in v0.0.5
func (invocable *Invocable) CallMethodByNameWithArgInterceptor(methodName string, container *ContainerInstance, interceptor FuncArgResolverInterceptor, parameters ...any) []reflect.Value
func (*Invocable) CallMethodWith ¶ added in v0.0.3
func (invocable *Invocable) CallMethodWith(container *ContainerInstance, parameters ...any) []reflect.Value
CallMethodWith - Call the method and assign its parameters from the passed parameters & container
func (*Invocable) InstantiateStructAndFill ¶ added in v0.0.5
func (invocable *Invocable) InstantiateStructAndFill(container *ContainerInstance) reflect.Value
func (*Invocable) InstantiateWith ¶ added in v0.0.3
func (invocable *Invocable) InstantiateWith(container *ContainerInstance) any
InstantiateWith - Instantiate a struct and fill its fields with values from the container