Documentation
¶
Overview ¶
Package inject make your dependency injection easy. Container allows you to inject dependencies into constructors or structures without the need to have specified each argument manually.
Provide ¶
First of all, when creating a new container, you need to describe how to create each instance of a dependency. To do this, use the container option inject.Provide().
container := New(
Provide(NewDependency),
Provide(NewAnotherDependency)
)
func NewDependency(dependency *pkg.AnotherDependency) *pkg.Dependency {
return &pkg.Dependency{
dependency: dependency,
}
}
func NewAnotherDependency() (*pkg.AnotherDependency, error) {
if dependency, err = initAnotherDependency(); err != nil {
return nil, err
}
return dependency, nil
}
Now, container knows how to create *pkg.Dependency and *pkg.AnotherDependency. For advanced providing see inject.Provide() and inject.ProvideOption documentation.
Extract ¶
After building a container, it is easy to get any previously provided type. To do this, use the container's Extract() method.
var anotherDependency *pkg.AnotherDependency
if err = container.Extract(&anotherDependency); err != nil {
// handle error
}
The container collects a dependencies of *pkg.AnotherDependency, creates its instance and places it in a target pointer. For advanced extraction see Extract() and inject.ExtractOption documentation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is a dependency injection container.
func (*Container) Extract ¶
func (c *Container) Extract(target interface{}, options ...ExtractOption) (err error)
Extract populates given target pointer with type instance provided in the container.
var server *http.Server
if err = container.Extract(&server); err != nil {
// extract failed
}
If the target type does not exist in a container or instance type building failed, Extract() returns an error. Use ExtractOption for modifying the behavior of this function.
type ExtractOption ¶
type ExtractOption interface {
// contains filtered or unexported methods
}
ExtractOption modifies default extract behavior. See inject.Name().
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures container. See inject.Provide(), inject.Bundle(), inject.Replace().
func Bundle ¶
Bundle group together container options.
accountBundle := inject.Bundle( inject.Provide(NewAccountController), inject.Provide(NewAccountRepository), ) authBundle := inject.Bundle( inject.Provide(NewAuthController), inject.Provide(NewAuthRepository), ) container, _ := New( accountBundle, authBundle, )
func Provide ¶
func Provide(provider interface{}, options ...ProvideOption) Option
Provide returns container option that explains how to create an instance of a type inside a container.
The first argument is the constructor function. A constructor is a function that creates an instance of the required type. It can take an unlimited number of arguments needed to create an instance - the first returned value.
func NewServer(mux *http.ServeMux) *http.Server {
return &http.Server{
Handle: mux,
}
}
Optionally, you can return a cleanup function and initializing error.
func NewServer(mux *http.ServeMux) (*http.Server, cleanup func(), err error) {
if time.Now().Day = 1 {
return nil, nil, errors.New("the server is down on the first day of a month")
}
server := &http.Server{
Handler: mux,
}
cleanup := func() {
_ = server.Close()
}
return &server, cleanup, nil
}
Other function signatures will cause error.
type ParameterBag ¶
type ParameterBag map[string]interface{}
ParameterBag is a provider parameter bag. It stores a construction parameters.
inject.Provide(NewServer, inject.ParameterBag{
"addr": ":8080",
})
NewServer(pb inject.ParameterBag) *http.Server {
return &http.Server{
Addr: pb.RequireString("addr"),
}
}
type ProvideOption ¶
type ProvideOption interface {
// contains filtered or unexported methods
}
ProvideOption modifies default provide behavior. See inject.WithName(), inject.As(), inject.Prototype().
func As ¶
func As(ifaces ...interface{}) ProvideOption
As specifies interfaces that implement provider instance. Provide with As() automatically checks that constructor result implements interface and creates slice group with it.
Provide(&http.ServerMux{}, inject.As(new(http.Handler)))
var handler http.Handler
container.Extract(&handler) // extract as interface
var handlers []http.Handler
container.Extract(&handlers) // extract group
func Prototype ¶
func Prototype() ProvideOption
Prototype modifies Provide() behavior. By default, each type resolves as a singleton. This option sets that each type resolving creates a new instance of the type.
Provide(&http.Server{], inject.Prototype())
var server1 *http.Server
container.Extract(&server1, &server1)
func WithName ¶
func WithName(name string) ProvideOption
WithName sets string identifier for provided value.
inject.Provide(&http.Server{}, inject.WithName("first"))
inject.Provide(&http.Server{}, inject.WithName("second"))
container.Extract(&server, inject.Name("second"))
