container

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 6 Imported by: 0

README

// Author: Daniel TAN // Date: 2021-12-18 00:22:22 // LastEditors: Daniel TAN // LastEditTime: 2021-12-18 00:23:29 // FilePath: /trinity-micro/core/ioc/container/README.go // Description:

container

the container package provide the dependency injection

Overview

NewContainer

new a container instance

// new container with default config 
s := NewContainer()

// new container with customize config 
s := NewContainer(Config{
	// the default value of autowired 
    // default true
	AutoWire        bool
    // replace the log
    // default logrus.New()  
	Log              : logrus.New(),
    // replace the container keyword
    // default container  
	ContainerKeyword : Keyword("container"),  
    // replace the autowire keyword
    // default autowire  
	AutowireKeyword  : Keyword("autowire"),  
    // replace the resource keyword
    // default resource 
	ResourceKeyword  : Keyword("resource"),   
})
RegisterInstance (Singleton)

register a singleton instance.

Only can register one kinds of instance, singleton or multi-instance
instance1:= UserService{}
s := NewContainer()
s.RegisterInstance("instance1", &instance1)
RegisterInstance (Multi-instance)

register a multi-instance instance.

Only can register one kinds of instance, singleton or multi-instance
instancePool1:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
s := NewContainer()
s.RegisterInstancePool("instance1", instancePool1)
InstanceDISelfCheck

do the di self check

if the registered instance's param with autoware not be injected, will return error
  • success case
type UserService struct {
    UserRepo UserRepo `container:"autowire:true;resource:UserRepo"`
}

UserServicePool:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
UserRepoPool:= Sync.Pool{
    New: func() interface{}{
        return new(UserRepo)
    }
}
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
s.RegisterInstancePool("UserRepo", UserRepoPool)
err := s.InstanceDISelfCheck() // err is nil 
  • failed case
type UserService struct {
    UserRepo UserRepo `container:"autowire:true;resource:UserRepo"`
}

UserServicePool:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
err := s.InstanceDISelfCheck() // err is not nil
// cause autowire is true, but there is no instance "UserRepo" registered in container
GetInstance

get an instance from container

  • normal case
UserServicePool:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
UserService := s.GetInstance("UserService")
  • inject with existing instance
UserServicePool:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
// used to handle the Circular References.
// always inject with the existing instance as priority
UserService := s.GetInstance("UserService", map[string]interface{}{
    "UserRepo":&UserRepo{},
})
Release (multi instance)

release the instance to the pool and clean all the param which has been injected only effective in multi-instance

type UserService struct {
    UserRepo UserRepo `container:"autowire:true;resource:UserRepo"`
}

UserServicePool:= Sync.Pool{
    New: func() interface{}{
        return new(UserService)
    }
}
UserRepoPool:= Sync.Pool{
    New: func() interface{}{
        return new(UserRepo)
    }
}
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
s.RegisterInstancePool("UserRepo", UserRepoPool)
UserService := s.GetInstance("UserService")
s.Release("UserService",UserService)
// s.Release("UserRepo",UserService.UserRepo ) 
// s.Release("UserService",UserService ) 
Release (multi instance)

Getting Started

  • How to use Container pkg
s := NewContainer()
s.RegisterInstancePool("UserService", UserServicePool)
s.RegisterInstancePool("UserRepo", UserRepoPool)
if err := s.InstanceDISelfCheck; err != nil {
    log.Fatal("di self check failed")
}
instance := s.GetInstance("instance")
defer s.Release("instance", instance)
// service logic 
instance.XXX 

Contributing

Feel free to create the Issue and PR . We need your help !

Documentation

Overview

Author: Daniel TAN Date: 2021-08-18 23:32:00 LastEditors: Daniel TAN LastEditTime: 2021-12-17 23:50:47 FilePath: /trinity-micro/core/ioc/container/consts.go Description:

Author: Daniel TAN Date: 2021-08-18 23:32:00 LastEditors: Daniel TAN LastEditTime: 2021-12-18 00:02:05 FilePath: /trinity-micro/core/ioc/container/util.go Description:

Index

Constants

View Source
const (
	TAG_SPLITER    = ";"
	TAG_KV_SPLITER = ":"
	CONTEXT        = "CONTEXT"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// AutoWired
	// the default value of autowired
	AutoWire         bool
	Log              logrus.FieldLogger
	ContainerKeyword Keyword
	AutowireKeyword  Keyword
	ResourceKeyword  Keyword
}

type Container

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

func NewContainer

func NewContainer(c ...Config) *Container

NewContainer get the new container instance if not passing the config , will init with the default config

func (*Container) CheckInstanceNameIfExist

func (s *Container) CheckInstanceNameIfExist(instanceName string) bool

CheckInstanceNameIfExist check instance name if exist if exist , return true if not exist , return false

func (*Container) DiAllFields

func (s *Container) DiAllFields(dest interface{}, injectingMap map[string]interface{})

func (*Container) DiFree added in v0.1.12

func (s *Container) DiFree(dest interface{})

func (*Container) DiSelfCheck

func (s *Container) DiSelfCheck(instanceName string) error

DiSelfCheck check if the registered instance is invalid

func (*Container) GetInstance

func (s *Container) GetInstance(instanceName string, injectingMap map[string]interface{}) interface{}

InstanceDISelfCheck get instance by instance name injectingMap , the dependency instance, will inject the instance in injectingMap as priority

func (*Container) InstanceDISelfCheck

func (s *Container) InstanceDISelfCheck() error

InstanceDISelfCheck self check all the instance registered exist or not

func (*Container) RegisterInstance

func (s *Container) RegisterInstance(instanceName string, instancePool *sync.Pool)

RegisterInstance register new instance if instanceName is empty will fatal if instancePool is invalid , will fatal

func (*Container) Release

func (s *Container) Release(instanceName string, instance interface{})

Release release the instance to instance pool

type Keyword

type Keyword string

Jump to

Keyboard shortcuts

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