Documentation ¶
Overview ¶
Package inject implements dependency injection.
Dependency injection with the struct tag `inject:""` or the constructor.
Dependency injection in Go ¶
Dependency injection is a concept valid for any programming language. The general concept behind dependency injection is called Inversion of Control. According to this concept a struct should not configure its dependencies statically but should be configured from the outside.
Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.
A Go struct has a dependency on another struct, if it uses an instance of this struct. We call this a struct dependency. For example, a struct which accesses a user controller has a dependency on user service struct.
Ideally Go struct should be as independent as possible from other Go struct. This increases the possibility of reusing these struct and to be able to test them independently from other struct.
The following example shows a struct which has no hard dependencies.
Example ¶
This example shows that the dependency is injected through the constructor
package main import ( "github.com/hidevopsio/hiboot/pkg/app" "github.com/hidevopsio/hiboot/pkg/app/web" ) // This example shows that the dependency is injected through the constructor func main() { } // HelloService is a simple service interface, with interface, we can mock a fake service in unit test type HelloService interface { SayHello(name string) string } type helloServiceImpl struct { } func init() { // Register Rest Controller through constructor newHelloController web.RestController(newHelloController) // Register Service through constructor newHelloService app.Component(newHelloService) } // please note that the return type name of the constructor HelloService, // hiboot will instantiate a instance named helloService for dependency injection func newHelloService() HelloService { return &helloServiceImpl{} } // SayHello is a service method implementation func (s *helloServiceImpl) SayHello(name string) string { return "Hello" + name } // PATH: /login type helloController struct { web.Controller helloService HelloService } // Init inject jwtToken through the argument jwtToken jwt.Token on constructor func newHelloController(helloService HelloService) *helloController { return &helloController{ helloService: helloService, } } // Get / // The first word of method name is the http method GET func (c *helloController) Get(name string) string { return c.helloService.SayHello(name) } // run above controller as a web app func main() { web.NewApplication().Run() }
Output:
Index ¶
- Variables
- func AddTag(tag Tag)
- func DefaultValue(object interface{}) error
- func IntoFunc(object interface{}) (retVal interface{}, err error)
- func IntoObject(object interface{}) error
- func IntoObjectValue(object reflect.Value, tags ...Tag) error
- func SetFactory(f factory.ConfigurableFactory)
- type BaseTag
- func (t *BaseTag) Decode(object reflect.Value, field reflect.StructField, tag string) (retVal interface{})
- func (t *BaseTag) Init(systemConfig *system.Configuration, configurations cmap.ConcurrentMap)
- func (t *BaseTag) IsSingleton() bool
- func (t *BaseTag) ParseProperties(tag string) cmap.ConcurrentMap
- func (t *BaseTag) Properties() cmap.ConcurrentMap
- type Tag
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // NotImplementedError: the interface is not implemented NotImplementedError = errors.New("[inject] interface is not implemented") // InvalidObjectError: the object is invalid InvalidObjectError = errors.New("[inject] invalid object") // InvalidTagNameError the tag name is invalid InvalidTagNameError = errors.New("[inject] invalid tag name, e.g. exampleTag") // SystemConfigurationError system is not configured SystemConfigurationError = errors.New("[inject] system is not configured") // InvalidInputError InvalidInputError = errors.New("[inject] invalid input") // InvalidFuncError InvalidFuncError = errors.New("[inject] invalid func") // FactoryIsNilError FactoryIsNilError = errors.New("[inject] factory is nil") )
Functions ¶
func DefaultValue ¶ added in v0.6.2
func DefaultValue(object interface{}) error
DefaultValue injects instance into the tagged field with `inject:"instanceName"`
func IntoFunc ¶ added in v0.6.0
func IntoFunc(object interface{}) (retVal interface{}, err error)
IntoFunc inject object into func and return instance
func IntoObject ¶
func IntoObject(object interface{}) error
IntoObject injects instance into the tagged field with `inject:"instanceName"`
func IntoObjectValue ¶ added in v0.5.0
IntoObjectValue injects instance into the tagged field with `inject:"instanceName"`
func SetFactory ¶ added in v0.5.4
func SetFactory(f factory.ConfigurableFactory)
SetFactory set factory from app
Types ¶
type BaseTag ¶ added in v0.3.0
type BaseTag struct {
// contains filtered or unexported fields
}
func (*BaseTag) Decode ¶ added in v0.3.0
func (*BaseTag) Init ¶ added in v0.5.0
func (t *BaseTag) Init(systemConfig *system.Configuration, configurations cmap.ConcurrentMap)
func (*BaseTag) ParseProperties ¶ added in v0.3.0
func (t *BaseTag) ParseProperties(tag string) cmap.ConcurrentMap
func (*BaseTag) Properties ¶ added in v0.3.0
func (t *BaseTag) Properties() cmap.ConcurrentMap
type Tag ¶ added in v0.3.0
type Tag interface { Init(systemConfig *system.Configuration, configurations cmap.ConcurrentMap) Decode(object reflect.Value, field reflect.StructField, tag string) (retVal interface{}) Properties() cmap.ConcurrentMap IsSingleton() bool }