Documentation ¶
Overview ¶
Package pkg is a framework that makes easy using the dependency injection in Golang.
The main purpose of using Autowire as dependency injection framework is to eliminate usage of globals without the tedious approach to manually wiring all the dependencies together. Current approach of Autowire framework is relying on dependency injection via using struct tags and reflection. All the dependencies are injected via golang reflection.
Basic usage is explained in the package-level example below. If you're new to Autowire, start there!
Testing of Autowire Applications ¶
To write unit or end-to-end tests of your application, you can use functions provided by atesting package. For more information take a look at https://godoc.org/github.com/go-autowire/autowire/atesting.
Index ¶
Constants ¶
const Tag = "autowire"
Tag name
Variables ¶
This section is empty.
Functions ¶
func Autowire ¶
func Autowire(v interface{})
Autowire function injects all dependencies for the given structure v. Autowire function should be executed in the init() function of the package. In order dependencies to be injected the desired struct fields should be marked with autowire tag.
Concrete type Injection ¶
Currently both type of fields exported and unexported are supported.
Unexported field Following snippet shows injecting dependencies inside private structure fields using `autowire:""` tag:
type Application struct { config *configuration.ApplicationConfig `autowire:""` }
Exported field The use of upper-case names of the struct fields indicated that field is exported.
type Application struct { Config *configuration.ApplicationConfig `autowire:""` }
It is important to note that only struct pointers are supported.
Interface Injection ¶
Often it's better to rely on interface instead of concrete type, in order to accomplish this decoupling we should specify interfaces as a type of struct fields. The following snippet demonstrate that:
type UserService struct { userRoleRepository UserRoleRepository `autowire:"repository/InMemoryUserRoleRepository"` }
UserRoleRepository is simply an interface and InMemoryUserRoleRepository is a struct, which implements that interface. For more information take a look at example https://github.com/go-autowire/autowire/tree/main/example package. Very Simplified Example:
type App struct {} func init() { Autowire(&App{}) }
As mentioned above Autowire function should be invoked in the package init function, but also it is possible to do it in the main function of the application, or separate files, which autowire all the structs.
func Autowired ¶
func Autowired(v interface{}) interface{}
Autowired function returns fully initialized with all dependencies instance, which is ready to be used. As the result is empty interface, type assertions is required before using the instance. Take a look at https://golang.org/ref/spec#Type_assertions for more information. The following snippet demonstrate how could be done :
app := Autowired(app.Application{}).(*app.Application)
func Close ¶
func Close() []error
Close function invoke Close method on each autowired struct which implements io.Closer interface, so currently active occupied resources (connections, channels, descriptor, etc.) could be released. Returning slice of occurred errors. Close functions cleans the dependency graph.
Types ¶
This section is empty.
Directories ¶
Path | Synopsis |
---|---|
Package atesting provides Spy function for easy way to mock dependencies
|
Package atesting provides Spy function for easy way to mock dependencies |
Package internal holds unexported api code
|
Package internal holds unexported api code |
fake
Package fake holds test structs
|
Package fake holds test structs |