Documentation ¶
Index ¶
- type Container
- type DefaultServiceProvider
- type ExtractOption
- type IServiceProvider
- type IServiceProviderFactory
- type Option
- type ParameterBag
- type ProvideOption
- type ServiceCollection
- func (sc *ServiceCollection) AddServiceDescriptor(sd *ServiceDescriptor)
- func (sc *ServiceCollection) AddSingleton(provider interface{})
- func (sc *ServiceCollection) AddSingletonAndName(name string, provider interface{})
- func (sc *ServiceCollection) AddSingletonByImplements(provider interface{}, implements interface{})
- func (sc *ServiceCollection) AddSingletonByImplementsAndName(name string, provider interface{}, implements interface{})
- func (sc *ServiceCollection) AddSingletonByName(name string, provider interface{})
- func (sc *ServiceCollection) AddSingletonByNameAndImplements(name string, provider interface{}, implements interface{})
- func (sc *ServiceCollection) AddTransient(provider interface{})
- func (sc *ServiceCollection) AddTransientByImplements(provider interface{}, implements interface{})
- func (sc *ServiceCollection) AddTransientByName(name string, provider interface{})
- func (sc ServiceCollection) Build() IServiceProvider
- type ServiceDescriptor
- func NewServiceDescriptor(name string, provider interface{}, implements interface{}, ...) *ServiceDescriptor
- func NewServiceDescriptorByImplements(provider interface{}, implements interface{}, lifetime ServiceLifetime) *ServiceDescriptor
- func NewServiceDescriptorByName(name string, provider interface{}, lifetime ServiceLifetime) *ServiceDescriptor
- func NewServiceDescriptorByProvider(provider interface{}, lifetime ServiceLifetime) *ServiceDescriptor
- type ServiceLifetime
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 DefaultServiceProvider ¶
type DefaultServiceProvider struct {
// contains filtered or unexported fields
}
func (DefaultServiceProvider) GetGraph ¶
func (d DefaultServiceProvider) GetGraph() string
func (DefaultServiceProvider) GetService ¶
func (d DefaultServiceProvider) GetService(refObject interface{}) (err error)
func (DefaultServiceProvider) GetServiceByName ¶
func (d DefaultServiceProvider) GetServiceByName(refObject interface{}, name string) (err error)
func (DefaultServiceProvider) InvokeService ¶
func (d DefaultServiceProvider) InvokeService(fn interface{}) error
type ExtractOption ¶
type ExtractOption interface {
// contains filtered or unexported methods
}
ExtractOption modifies default extract behavior. See inject.Name().
type IServiceProvider ¶
type IServiceProviderFactory ¶
type IServiceProviderFactory interface {
CreateServiceProvider() IServiceProvider
}
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. It is a alternative way to configure type.
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 var server2 *http.Server container.Extract(&server1, &server2)
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"))
type ServiceCollection ¶
type ServiceCollection struct {
// contains filtered or unexported fields
}
func NewServiceCollection ¶
func NewServiceCollection() *ServiceCollection
func (*ServiceCollection) AddServiceDescriptor ¶
func (sc *ServiceCollection) AddServiceDescriptor(sd *ServiceDescriptor)
Singleton Scoped Transient
func (*ServiceCollection) AddSingleton ¶
func (sc *ServiceCollection) AddSingleton(provider interface{})
func (*ServiceCollection) AddSingletonAndName ¶
func (sc *ServiceCollection) AddSingletonAndName(name string, provider interface{})
func (*ServiceCollection) AddSingletonByImplements ¶
func (sc *ServiceCollection) AddSingletonByImplements(provider interface{}, implements interface{})
func (*ServiceCollection) AddSingletonByImplementsAndName ¶
func (sc *ServiceCollection) AddSingletonByImplementsAndName(name string, provider interface{}, implements interface{})
func (*ServiceCollection) AddSingletonByName ¶
func (sc *ServiceCollection) AddSingletonByName(name string, provider interface{})
func (*ServiceCollection) AddSingletonByNameAndImplements ¶
func (sc *ServiceCollection) AddSingletonByNameAndImplements(name string, provider interface{}, implements interface{})
func (*ServiceCollection) AddTransient ¶
func (sc *ServiceCollection) AddTransient(provider interface{})
func (*ServiceCollection) AddTransientByImplements ¶
func (sc *ServiceCollection) AddTransientByImplements(provider interface{}, implements interface{})
func (*ServiceCollection) AddTransientByName ¶
func (sc *ServiceCollection) AddTransientByName(name string, provider interface{})
func (ServiceCollection) Build ¶
func (sc ServiceCollection) Build() IServiceProvider
type ServiceDescriptor ¶
type ServiceDescriptor struct { Name string Provider interface{} Implements interface{} Lifetime ServiceLifetime }
func NewServiceDescriptor ¶
func NewServiceDescriptor(name string, provider interface{}, implements interface{}, lifetime ServiceLifetime) *ServiceDescriptor
func NewServiceDescriptorByImplements ¶
func NewServiceDescriptorByImplements(provider interface{}, implements interface{}, lifetime ServiceLifetime) *ServiceDescriptor
func NewServiceDescriptorByName ¶
func NewServiceDescriptorByName(name string, provider interface{}, lifetime ServiceLifetime) *ServiceDescriptor
func NewServiceDescriptorByProvider ¶
func NewServiceDescriptorByProvider(provider interface{}, lifetime ServiceLifetime) *ServiceDescriptor
type ServiceLifetime ¶
type ServiceLifetime int32
const ( Singleton ServiceLifetime = 0 Scoped ServiceLifetime = 1 Transient ServiceLifetime = 2 )