Documentation
¶
Overview ¶
Package di provides opinionated way to connect your application components. Container allows you to inject dependencies into constructors or structures without the need to have specified each argument manually.
Index ¶
- type CompileOption
- type Constructor
- type Container
- func (c *Container) Cleanup()
- func (c *Container) Compile(_ ...CompileOption) error
- func (c *Container) Has(into interface{}, options ...ResolveOption) bool
- func (c *Container) Invoke(invocation Invocation, options ...InvokeOption) error
- func (c *Container) Provide(constructor Constructor, options ...ProvideOption) error
- func (c *Container) Resolve(into interface{}, options ...ResolveOption) error
- type ErrInvokeFailed
- type ErrProvideFailed
- type ErrResolveFailed
- type Inject
- type Interface
- type Invocation
- type InvokeOption
- type InvokeParams
- type Logger
- type Option
- type ProvideOption
- type ProvideParams
- type ResolveOption
- type ResolveParams
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CompileOption ¶
type CompileOption interface {
// contains filtered or unexported methods
}
CompileOption modifies compile behaviour. Deprecated: Compile deprecated: https://github.com/goava/di/pull/9
type Constructor ¶
type Constructor interface{}
Constructor is a function with follow signature:
func NewHTTPServer(addr string, handler http.Handler) (server *http.Server, cleanup func(), err error) {
server := &http.Server{
Addr: addr,
}
cleanup = func() {
server.Close()
}
return server, cleanup, nil
}
This constructor function teaches container how to build server. Arguments (addr and handler) in this function is a dependencies. They will be resolved automatically when someone needs a server. Constructor may have unlimited count of dependencies, but note that container should know how build each of them. Second result of this function is a optional cleanup callback. It describes that container will do on shutdown. Third result is a optional error. Sometimes our types cannot be constructed.
type Container ¶
type Container struct {
// contains filtered or unexported fields
}
Container is a dependency injection container.
func New ¶
New constructs container with provided options. Example usage (simplified):
Define constructors and invocations:
func NewHTTPServer(mux *http.ServeMux) *http.Server {
return &http.Server{
Handler: mux,
}
}
func NewHTTPServeMux() *http.ServeMux {
return http.ServeMux{}
}
func StartServer(server *http.Server) error {
return server.ListenAndServe()
}
Use it with container:
container, err := di.New(
di.Provide(NewHTTPServer),
di.Provide(NewHTTPServeMux),
di.Invoke(StartServer),
)
if err != nil {
// handle error
}
func (*Container) Cleanup ¶
func (c *Container) Cleanup()
Cleanup runs destructors in reverse order that was been created.
func (*Container) Compile ¶
func (c *Container) Compile(_ ...CompileOption) error
Compile compiles the container. Deprecated: Compile deprecated: https://github.com/goava/di/pull/9
func (*Container) Has ¶ added in v1.0.0
func (c *Container) Has(into interface{}, options ...ResolveOption) bool
Has checks that type exists in container, if not it return false.
var server *http.Server
if container.Has(&server) {
// handle server existence
}
It like Resolve() but doesn't instantiate a type.
func (*Container) Invoke ¶
func (c *Container) Invoke(invocation Invocation, options ...InvokeOption) error
Invoke calls the function fn. It parses function parameters. Looks for it in a container. And invokes function with them. See Invocation for details.
func (*Container) Provide ¶
func (c *Container) Provide(constructor Constructor, options ...ProvideOption) error
Provide provides to container reliable way to build type. The constructor will be invoked lazily on-demand. For more information about constructors see Constructor interface. ProvideOption can add additional behavior to the process of type resolving.
func (*Container) Resolve ¶
func (c *Container) Resolve(into interface{}, options ...ResolveOption) error
Resolve resolves type and fills target pointer.
var server *http.Server
if err := container.Resolve(&server); err != nil {
// handle error
}
type ErrInvokeFailed ¶ added in v1.2.0
type ErrInvokeFailed struct {
// contains filtered or unexported fields
}
ErrInvokeFailed causes when invoke failed.
func (ErrInvokeFailed) Error ¶ added in v1.2.0
func (e ErrInvokeFailed) Error() string
Error returns error string.
type ErrProvideFailed ¶ added in v1.2.0
type ErrProvideFailed struct {
// contains filtered or unexported fields
}
ErrProvideFailed causes when constructor providing failed.
func (ErrProvideFailed) Error ¶ added in v1.2.0
func (e ErrProvideFailed) Error() string
Error returns error string.
type ErrResolveFailed ¶ added in v1.2.0
type ErrResolveFailed struct {
// contains filtered or unexported fields
}
ErrResolveFailed causes when type resolve failed.
func (ErrResolveFailed) Error ¶ added in v1.2.0
func (e ErrResolveFailed) Error() string
Error returns error string.
type Inject ¶ added in v1.1.0
type Inject struct {
// contains filtered or unexported fields
}
Inject indicates that public fields with special tag type will be injected automatically.
type MyType struct {
di.Inject
Server *http.Server `di:""` // will be injected
}
type Interface ¶
type Interface interface{}
Interface is a pointer to interface, like new(http.Handler). Tell container that provided type may be used as interface.
type Invocation ¶
type Invocation interface{}
Invocation is a function whose signature looks like:
func StartServer(server *http.Server) error {
return server.ListenAndServe()
}
Like a constructor invocation may have unlimited count of arguments and they will be resolved automatically. The invocation can return an optional error. Error will be returned as is.
type InvokeOption ¶
type InvokeOption interface {
// contains filtered or unexported methods
}
InvokeOption is a functional option interface that modify invoke behaviour.
type InvokeParams ¶
type InvokeParams struct {
// The function
Fn interface{}
}
InvokeParams is a invoke parameters.
type Logger ¶ added in v1.0.0
type Logger interface {
Logf(format string, values ...interface{})
}
Logger logs internal container actions. By default it omits logs. You can set logger by Option LogFunc(). Also, you can provide you own logger to container as di.Logger interface. Then container use it for internal logs.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a functional option that configures container. If you don't know about functional options, see https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis. Below presented all possible options with their description:
- di.Provide - provide constructors
- di.Invoke - add invocations
- di.Resolve - resolves type
func Invoke ¶
func Invoke(fn Invocation, options ...InvokeOption) Option
Invoke returns container option that registers container invocation. All invocations will be called on di.New() after processing di.Provide() options. See Container.Invoke() for details.
func Options ¶
Options group together container options.
account := di.Options(
di.Provide(NewAccountController),
di.Provide(NewAccountRepository),
)
auth := di.Options(
di.Provide(NewAuthController),
di.Provide(NewAuthRepository),
)
container, err := di.New(
account,
auth,
)
if err != nil {
// handle error
}
func Provide ¶
func Provide(constructor Constructor, options ...ProvideOption) Option
Provide returns container option that provides to container reliable way to build type. The constructor will be invoked lazily on-demand. For more information about constructors see Constructor interface. ProvideOption can add additional behavior to the process of type resolving.
func Resolve ¶
func Resolve(target interface{}, options ...ResolveOption) Option
Resolve returns container options that resolves type into target. All resolves will be done on compile stage after call invokes.
func WithCompile ¶ added in v1.0.0
func WithCompile() Option
WithCompile ejects compile stage. Deprecated: Compile deprecated: https://github.com/goava/di/pull/9
func WithLogger ¶ added in v1.0.0
WithLogger sets container logger.
type ProvideOption ¶
type ProvideOption interface {
// contains filtered or unexported methods
}
ProvideOption is a functional option interface that modify provide behaviour. See di.As(), di.WithName().
func As ¶
func As(interfaces ...Interface) ProvideOption
As returns provide option that specifies interfaces for constructor resultant type.
INTERFACE USAGE:
You can provide type as interface and resolve it later without using of direct implementation. This creates less cohesion of code and promotes be more testable.
Create type constructors:
func NewServeMux() *http.ServeMux {
return &http.ServeMux{}
}
func NewServer(handler *http.Handler) *http.Server {
return &http.Server{
Handler: handler,
}
}
Build container with di.As provide option:
container, err := di.New(
di.Provide(NewServer),
di.Provide(NewServeMux, di.As(new(http.Handler)),
)
if err != nil {
// handle error
}
var server *http.Server
if err := container.Resolve(&http.Server); err != nil {
// handle error
}
In this example you can see how container inject type *http.ServeMux as http.Handler interface into the server constructor.
GROUP USAGE:
Container automatically creates group for interfaces. For example, you can use type []http.Handler in previous example.
var handlers []http.Handler
if err := container.Resolve(&handlers); err != nil {
// handle error
}
Container checks that provided type implements interface if not cause compile error.
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.
container, err := di.New(
Provide(NewHTTPServer, inject.Prototype())
)
if err != nil {
// handle error
}
var server1, server2 *http.Server
if err := container.Resolve(&server1); err != nil {
// handle error
}
if err := container.Resolve(&server2); err != nil {
// handle error
}
func WithName ¶
func WithName(name string) ProvideOption
WithName modifies Provide() behavior. It adds name identity for provided type.
type ProvideParams ¶
ProvideParams is a Provide() method options. Name is a unique identifier of type instance. Provider is a constructor function. Interfaces is a interface that implements a provider result type.
type ResolveOption ¶
type ResolveOption interface {
// contains filtered or unexported methods
}
ResolveOption is a functional option interface that modify resolve behaviour.
func Name ¶
func Name(name string) ResolveOption
Name specifies provider string identity. It needed when you have more than one definition of same type. You can identity type by name.
type ResolveParams ¶
type ResolveParams struct {
Name string
}
ResolveParams is a resolve parameters.