modulepackage
0.0.0-20181223232653-14e6c00f37ab
Repository: https://github.com/monnoroch/go-inject.git
Documentation: pkg.go.dev
# README
Go-inject — Dependency Injection Library for Go
A dependency injection system for Go inspired by Guice. See the post for detailed guide.
Installation
Go get
go get github.com/monnoroch/go-inject
Dep
dep ensure -add github.com/monnoroch/go-inject
Examples
Inject an int value
Here's a simple module with a single provider:
import (
"github.com/monnoroch/go-inject"
)
type singleValue struct{}
type MyModule struct{}
func (_ MyModule) ProvideValue() (int, singleValue) {
return 10, singleValue{}
}
func main() {
injector, _ := inject.InjectorOf(MyModule{})
// Will be 10.
value := injector.MustGet(new(int), singleValue{}).(int)
}
Dependencies
Let's add a second provider that depends on the first one:
type doubleValue struct{}
func (_ MyModule) ProvideValueDouble(value int, _ singleValue) (int, doubleValue) {
return value * 2, doubleValue{}
}
func main() {
injector, _ := inject.InjectorOf(MyModule{})
// Will be 20.
value := injector.MustGet(new(int), doubleValue{}).(int)
}
Cross-module dependencies
Let's add a second module, which can depend on values provided by the first module:
type tripleValue struct{}
type MyAnotherModule struct{}
func (_ MyAnotherModule) ProvideValue(
value int, _ singleValue,
doubledValue int, _ doubleValue,
) (int, tripleValue) {
return doubledValue + value, tripleValue{}
}
func main() {
injector, _ := inject.InjectorOf(
MyModule{},
MyAnotherModule{},
)
// Will be 30.
value := injector.MustGet(new(int), tripleValue{}).(int)
}
Lazy dependencies
You can inject a function that returns a value. That way the value will be computed lazily.
type tripleValueLazy struct{}
func (_ MyAnotherModule) ProvideValueLazy(
value int, _ singleValue,
doubledValue func() int, _ doubleValue,
) (int, tripleValueLazy) {
return doubledValue() + value, tripleValueLazy{}
}
func main() {
injector, _ := inject.InjectorOf(
MyModule{},
MyAnotherModule{},
)
// Will be 30.
value := injector.MustGet(new(int), tripleValueLazy{}).(int)
}
Auto injecting dependencies in struct fields
import (
"github.com/monnoroch/go-inject"
"github.com/monnoroch/go-inject/auto"
)
type MyStruct struct {
Value int
DoubledValue int
}
type myAnnotation struct{}
func main() {
injector, _ := inject.InjectorOf(
MyModule{},
autoinject.AutoInjectModule(new(MyStruct)).
WithAnnotation(myAnnotation{}).
WithFieldAnnotations(struct {
Value singleValue
DoubledValue doubleValue
}{}),
)
// Will be MyStruct{Value: 10, DoubledValue: 20}.
value := injector.MustGet(new(MyStruct), myAnnotation{}).(MyStruct)
}
You can also inplement autoinject.AutoInjectable
to provide default way to construct MyStruct
:
func (self MyStruct) ProvideAutoInjectAnnotations() interface{} {
return struct {
Value singleValue
DoubledValue doubleValue
}{}
}
func main() {
injector, _ := inject.InjectorOf(
MyModule{},
autoinject.AutoInjectModule(new(MyStruct)).
WithAnnotation(myAnnotation{}),
)
// Will be MyStruct{Value: 10, DoubledValue: 20}.
value := injector.MustGet(new(MyStruct), myAnnotation{}).(MyStruct)
}
You can also use the default autoinject.Auto
annotation to simplify code even further:
func main() {
injector, _ := inject.InjectorOf(
MyModule{},
autoinject.AutoInjectModule(new(MyStruct)),
)
// Will be MyStruct{Value: 10, DoubledValue: 20}.
value := injector.MustGet(new(MyStruct), autoinject.Auto{}).(MyStruct)
}
Other features
Providing singletons, private providers, annotation rewriting, dynamic modules and other features are explained in more detail in the guide.
# Packages
No description provided by the author
autoinject extends go-inject library with a way to automatically generate a provider for a struct type.
No description provided by the author
rewrite provides tools to dynamically transform modules to chenge their providers using reflection.
No description provided by the author
# Functions
CheckModule checks if the object is in fact a Module.
Combine multiple modules into one.
Create an injector from the list of modules.
Create a new provider from either a function or a `reflect.Value` with a function.
Get the list of providers from the module.
# Interfaces
Annotation is the interface that has to be implemented by all annotations.
Dynamic providers module.
Module is the interface that has to be implemented by all modules.