Documentation
¶
Overview ¶
contains the wire implementation of interfaces defined on pkg
Example ¶
package main
import (
"fmt"
wiring "github.com/4strodev/wiring/pkg"
)
type Abstraction interface {
Greet()
}
type Implementation struct {
}
func (i *Implementation) Greet() {
fmt.Println("Hello world")
}
func main() {
var container = wiring.New()
err := container.Singleton(func() (Abstraction, error) {
return &Implementation{}, nil
})
if err != nil {
panic(err)
}
var impl Abstraction
err = container.Resolve(&impl)
if err != nil {
panic(err)
}
impl.Greet()
}
Output: Hello world
Index ¶
Examples ¶
Constants ¶
View Source
const ( SINGLETON abstractionLifeCycle = iota TRANSIENT )
View Source
const WIRE_TAG = "wire"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container interface {
// Singleton sets a dependency as a [wiring.] dependency.
// Once the abstraction is instanciated this instance will be cached and
// will no longer create new instances
Singleton(resolver any) error
// Transient sets a dependency as a transient dependency.
// Every time the container is asked to resolve an abstraction
// the container will create a new instance of that dependency
Transient(resolver any) error
// Resolve given a pointer to value it will be resolved and the container
// will update the referenced value with the instance resolved
Resolve(value any) error
// SingletonToken same as Singleton but instead of using the type to identify
// the implementation it uses the token
SingletonToken(token string, resolver any) error
// TransientToken same as Transient but instead of using the type to identify
// the implementation it uses the token
TransientToken(token string, resolver any) error
// Gets the instance associated with the provided token
ResolveToken(token string, value any) error
// Fill gets a struct pointer and resolves their fields, if the field needs to be resolved by token
// you can use the 'wire' tag with the token that is associated with. If the field needs to be ignored
// use the ignore param -> wire:",ignore". Unexported fields will be ignored
Fill(structure any) error
// Check if the container has a resolver for that type
HasType(refType reflect.Type) bool
// Check if the container has a resolver for that token
HasToken(token string) bool
}
Example (Fill) ¶
package main
import (
"fmt"
wiring "github.com/4strodev/wiring/pkg"
)
type Abstraction interface {
Greet()
}
type Implementation struct {
}
func (i *Implementation) Greet() {
fmt.Println("Hello world")
}
type StructFill struct {
Field string `wire:",ignore"`
Impl Abstraction
}
func main() {
var container = wiring.New()
err := container.Transient(func() (Abstraction, error) {
return &Implementation{}, nil
})
if err != nil {
panic(err)
}
var impl StructFill
err = container.Fill(&impl)
if err != nil {
panic(err)
}
impl.Impl.Greet()
}
Output: Hello world
Example (Singleton) ¶
package main
import (
"fmt"
wiring "github.com/4strodev/wiring/pkg"
)
type Abstraction interface {
Greet()
}
type Implementation struct {
}
func (i *Implementation) Greet() {
fmt.Println("Hello world")
}
func main() {
var container = wiring.New()
err := container.Singleton(func() (Abstraction, error) {
fmt.Println("Running resolver")
return &Implementation{}, nil
})
if err != nil {
panic(err)
}
var impl Abstraction
err = container.Resolve(&impl)
if err != nil {
panic(err)
}
impl.Greet()
var otherImpl Abstraction
err = container.Resolve(&otherImpl)
if err != nil {
panic(err)
}
otherImpl.Greet()
}
Output: Running resolver Hello world Hello world
Example (Token) ¶
package main
import (
"fmt"
wiring "github.com/4strodev/wiring/pkg"
)
type Abstraction interface {
Greet()
}
type Implementation struct {
}
func (i *Implementation) Greet() {
fmt.Println("Hello world")
}
func main() {
var container = wiring.New()
err := container.TransientToken("abstraction", func() (Abstraction, error) {
return &Implementation{}, nil
})
if err != nil {
panic(err)
}
var impl Abstraction
err = container.ResolveToken("abstraction", &impl)
if err != nil {
panic(err)
}
impl.Greet()
}
Output: Hello world
Example (Transient) ¶
package main
import (
"fmt"
wiring "github.com/4strodev/wiring/pkg"
)
type Abstraction interface {
Greet()
}
type Implementation struct {
}
func (i *Implementation) Greet() {
fmt.Println("Hello world")
}
func main() {
var container = wiring.New()
err := container.Transient(func() (Abstraction, error) {
// The resolver is executed every time the abstraction is resolved
fmt.Println("Running resolver")
return &Implementation{}, nil
})
if err != nil {
panic(err)
}
var impl Abstraction
err = container.Resolve(&impl)
if err != nil {
panic(err)
}
impl.Greet()
var otherImpl Abstraction
err = container.Resolve(&otherImpl)
if err != nil {
panic(err)
}
otherImpl.Greet()
}
Output: Running resolver Hello world Running resolver Hello world
Source Files
¶
Click to show internal directories.
Click to hide internal directories.