Documentation
¶
Overview ¶
Package syringe provide tools to build dependency graphs using constructors.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Default = New()
Default provide a quick access to an instanciated injector.
Functions ¶
This section is empty.
Types ¶
type Syringe ¶
type Syringe struct {
// contains filtered or unexported fields
}
Syringe is a dependency injector. The deps field contains the dependencies and constructors to inject.
func (*Syringe) Get ¶
Get injects the fields of an indirected struct passed as argument with dependencies corresponding to their type.
func (*Syringe) GetOne ¶
GetOne injects an empty pointer passed as argument with a dependency corresponding to its type.
func (*Syringe) Inject ¶
Inject builds the dependency graph. It is capable to resolve circular dependencies using stub injection.
Example:
dep1 struct {
dep *dep2
}
dep2 struct {
dep *dep1
}
func newDep1(dep *dep2) *dep1 {
return &dep1{dep: dep}
}
func newDep2(dep *dep1) *dep2 {
return &dep2{dep: dep}
}
In this example, the injector will call the two constructors with stub params and then will replace their value with the instanciated dependencies.
That method can be seen as not safe because it would override eventual modifications done in the constructors.
That being said, it feels like the standard way of doing things as it is a very specific problem that can't be fixed by a "by hand" injection. (True ?)
func (*Syringe) Register ¶
Register takes one, multiple, or a slice of dependencies and register them into the injector.
func (*Syringe) SafeInject ¶
SafeInject builds the dependency graph as a human would do by hand. Therefore, it is not capable to resolve circular dependencies.