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(target interface{}, options ...ResolveOption) bool
- func (c *Container) Invoke(fn Invocation, options ...InvokeOption) error
- func (c *Container) Provide(constructor Constructor, options ...ProvideOption) (err error)
- func (c *Container) Resolve(into interface{}, options ...ResolveOption) error
- type ErrParameterProvideFailed
- type ErrParameterProviderNotFound
- type Interface
- type Invocation
- type InvokeOption
- type InvokeParams
- type Logger
- type Option
- type Parameter
- 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 is a functional option that change compile behaviour.
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 closure. It describes what container will do on type destroying. 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 creates new container with provided options. Example usage:
func NewHTTPServer(mux *http.ServeMux) *http.Server {
return &http.Server{
Handler: mux,
}
}
func NewHTTPServeMux() *http.ServeMux {
return http.ServeMux{}
}
Container initialization code:
container, err := di.New(
di.Provide(NewHTTPServer),
di.Provide(NewHTTPServeMux),
)
if err != nil {
// handle error
}
var server *http.Server
if err := c.Resolve(&server); 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. First, it iterates over all definitions and register their parameters. Container links definitions with each other and checks that result dependency graph is not cyclic. In final, the container invoke functions provided by di.Invoke() container option and resolves types provided by di.Resolve() container option. Between invokes and resolves, the container tries to find di.Logger interface, and if it is found sets it as an internal logger.
func (*Container) Has ¶ added in v1.0.0
func (c *Container) Has(target interface{}, options ...ResolveOption) bool
Has checks that type exists in container, if not it return false.
func (*Container) Invoke ¶
func (c *Container) Invoke(fn Invocation, options ...InvokeOption) error
Invoke calls provided function.
func (*Container) Provide ¶
func (c *Container) Provide(constructor Constructor, options ...ProvideOption) (err 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 builds instance of target type and fills target pointer.
type ErrParameterProvideFailed ¶
type ErrParameterProvideFailed struct {
// contains filtered or unexported fields
}
ErrParameterProvideFailed causes when container found a provider but provide failed.
func (ErrParameterProvideFailed) Error ¶
func (e ErrParameterProvideFailed) Error() string
Error is a implementation of error interface.
type ErrParameterProviderNotFound ¶
type ErrParameterProviderNotFound struct {
// contains filtered or unexported fields
}
ErrParameterProviderNotFound causes when container could not found a provider for parameter.
func (ErrParameterProviderNotFound) Error ¶
func (e ErrParameterProviderNotFound) Error() string
Error is a implementation of error interface.
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 {
server.ListenAndServe()
}
Like a constructor invocation may have unlimited count of arguments and they will be resolved automatically. Also invocation may return optional error.
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 compile stage after dependency graph resolving.
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 puts the container compilation into a separate function.
func WithLogger ¶ added in v1.0.0
WithLogger sets container logger.
type Parameter ¶
type Parameter struct {
// contains filtered or unexported fields
}
Parameter is embed helper that indicates that type is a constructor embed parameter.
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() and di.Prototype().
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.