container

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2023 License: BSD-3-Clause Imports: 5 Imported by: 17

README

golang implement of IoC-Container

basic usage

package main
import (
    "fmt"
    "github.com/enorith/container"
    "reflect"
)

type Foo struct{
    name string
}

func main(){
    c := container.New()
 
    // bind
    c.BindFunc(Foo{}, func(c *container.Container) (reflect.Value, error) {
        return reflect.ValueOf(Foo{"foo"}), nil
    }, false)
    // get instance
    v, _ := c.Instance(Foo{})
    fmt.Println(v.Interface().(Foo).name)
    var f Foo
    // get instance
    c.InstanceFor(Foo{}, &f)
    fmt.Println(f.name)
    // bind with name
    c.BindFunc("foo", func(c *container.Container) (reflect.Value, error) {
        return reflect.ValueOf(Foo{"foo"}), nil
    }, false)
    v2, _ := c.Instance("foo")
    fmt.Println(v2.Interface().(Foo).name)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConditionInjectionFunc

type ConditionInjectionFunc func(abs interface{}) bool

ConditionInjectionFunc conditional injection function

type Container

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

Container is a IoC-Container

func New

func New() *Container

func (*Container) Bind

func (c *Container) Bind(abstract, instance interface{}, singleton bool)

Bind: pre-bind abstract to container

Abstract could be string,reflect.Type,struct or pointer
Instance could be reflect.Value, struct, pointer or InstanceRegister

func (*Container) BindFunc

func (c *Container) BindFunc(abs interface{}, register InstanceRegister, singleton bool)

func (*Container) Bound

func (c *Container) Bound(abs interface{}) bool

func (*Container) Clone added in v0.0.12

func (c *Container) Clone() Interface

func (*Container) GetRegisters

func (c *Container) GetRegisters() map[interface{}]InstanceRegister

func (*Container) InjectionCondition

func (c *Container) InjectionCondition(f ConditionInjectionFunc, i InjectionFunc)

func (*Container) InjectionRequire

func (c *Container) InjectionRequire(requireAbs interface{}, i InjectionFunc)

func (*Container) InjectionWith

func (c *Container) InjectionWith(i InjectionFunc)

func (*Container) Instance

func (c *Container) Instance(abs interface{}) (instance reflect.Value, e error)

Instance return reflect.Value of gaving abstract

func (*Container) InstanceFor

func (c *Container) InstanceFor(abs interface{}, out interface{}) error

func (*Container) Invoke

func (c *Container) Invoke(f interface{}) ([]reflect.Value, error)

func (*Container) IsSingleton

func (c *Container) IsSingleton(abs interface{}) bool

func (*Container) MethodCall

func (c *Container) MethodCall(abs interface{}, method string) ([]reflect.Value, error)

func (*Container) Register

func (c *Container) Register(instance interface{}, singleton bool)

func (*Container) RegisterSingleton

func (c *Container) RegisterSingleton(instance interface{})

func (*Container) Singleton

func (c *Container) Singleton(abs interface{}, instance interface{})

func (*Container) WithInjector

func (c *Container) WithInjector(h Injector)

type InjectPassedError added in v0.0.14

type InjectPassedError struct {
}

func (InjectPassedError) Error added in v0.0.14

func (InjectPassedError) Error() string

type InjectionFunc

type InjectionFunc func(abs interface{}, last reflect.Value) (reflect.Value, error)

InjectionFunc injection function

type Injector

type Injector interface {
	Injection(abs interface{}, last reflect.Value) (reflect.Value, error)
	When(abs interface{}) bool
}

Injector interface for conditional initializer

type InstanceRegister

type InstanceRegister func(c Interface) (interface{}, error)

InstanceRegister register instance for container

type Interface

type Interface interface {
	BindFunc(abs interface{}, register InstanceRegister, singleton bool)
	Bind(abstract, instance interface{}, singleton bool)
	Instance(abs interface{}) (instance reflect.Value, e error)
	InstanceFor(abs interface{}, out interface{}) error
	Invoke(f interface{}) ([]reflect.Value, error)
	MethodCall(abs interface{}, method string) ([]reflect.Value, error)
	RegisterSingleton(instance interface{})
	Register(instance interface{}, singleton bool)
	Singleton(abs interface{}, instance interface{})
	WithInjector(h Injector)
	InjectionWith(i InjectionFunc)
	InjectionRequire(requireAbs interface{}, i InjectionFunc)
	InjectionCondition(f ConditionInjectionFunc, i InjectionFunc)
	IsSingleton(abs interface{}) bool
	Bound(abs interface{}) bool
	GetRegisters() map[interface{}]InstanceRegister
	Clone() Interface
}

type UnregisterdAbstractError

type UnregisterdAbstractError struct {
	Abs string
}

func (UnregisterdAbstractError) Error

func (u UnregisterdAbstractError) Error() string

Jump to

Keyboard shortcuts

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