Documentation ¶
Overview ¶
Package gocontainer is a simple dependency injection container
Take the following example: First file `main.go` simply gets the repository from the container and prints it we use **MustInvoke** method to simply present the way where we keep type safety
package main import ( "github.com/vardius/gocontainer/example/repository" "github.com/vardius/gocontainer" ) func main() { gocontainer.MustInvoke("repository.mysql", func(r Repository) { fmt.Println(r) }) }
Our database implementation uses `init()` function to register db service
package database import ( "fmt" "database/sql" "github.com/vardius/gocontainer" ) func NewDatabase() *sql.DB { db, _ := sql.Open("mysql", "dsn") return db } func init() { db := gocontainer.MustGet("db") gocontainer.Register("db", NewDatabase()) }
Our repository accesses earlier on registered db service and following the same patter uses `init()` function to register repository service within container
package repository import ( "fmt" "database/sql" "github.com/vardius/gocontainer" _ "github.com/vardius/gocontainer/example/database" ) type Repository interface {} func NewRepository(db *sql.DB) Repository { return &mysqlRepository{db} } type mysqlRepository struct { db *sql.DB } func init() { db := gocontainer.MustGet("db") gocontainer.Register("repository.mysql", NewRepository(db.(*sql.DB))) }
You can disable global container instance by setting gocontainer.InitializeGlobalContainer to false. This package allows you to create many containers.
package main import ( "github.com/vardius/gocontainer/example/repository" "github.com/vardius/gocontainer" ) // disable global container instance // remember to do it outside function body gocontainer.InitializeGlobalContainer = false func main() { mycontainer := gocontainer.New() mycontainer.Register("test", 1) }
Example (Deregister) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) gocontainer.Deregister("test") fmt.Println(gocontainer.Get("test")) }
Output: <nil> false
Example (Get) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) o, ok := gocontainer.Get("test") fmt.Println(o) fmt.Println(ok) }
Output: 1 true
Example (Has) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) fmt.Println(gocontainer.Has("test")) }
Output: true
Example (Invoke) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) gocontainer.Invoke("test", func(i int, ok bool) { fmt.Println(i) fmt.Println(ok) }) }
Output: 1 true
Example (MustGet) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) o := gocontainer.MustGet("test") fmt.Println(o) }
Output: 1
Example (MustInvoke) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) gocontainer.MustInvoke("test", func(i int) { fmt.Println(i) }) }
Output: 1
Example (MustInvokeMany) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test1", 1) gocontainer.Register("test2", 2) gocontainer.MustInvokeMany("test1", "test2")(func(x int, y int) { fmt.Println(x, "|", y) }) }
Output: 1 | 2
Example (New) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { // disable global container instance // remember to do it outside function body gocontainer.InitializeGlobalContainer = false mycontainer := gocontainer.New() mycontainer.Register("test", 1) fmt.Println(mycontainer.MustGet("test")) }
Output: 1
Example (Register) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) fmt.Println(gocontainer.MustGet("test")) }
Output: 1
Index ¶
- Variables
- func Deregister(id string)
- func Get(id string) (interface{}, bool)
- func Has(id string) bool
- func Invoke(id string, fn interface{})
- func MustGet(id string) interface{}
- func MustInvoke(id string, fn interface{})
- func MustInvokeMany(ids ...string) func(fn interface{})
- func Register(id string, object interface{})
- type Container
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // InitializeGlobalContainer enables global container instance // instance will be initialized with package import by init() function InitializeGlobalContainer = true )
Functions ¶
func Invoke ¶
func Invoke(id string, fn interface{})
Invoke gets a service safely typed by passing it to a closure will panic if callback is not a function
func MustGet ¶
func MustGet(id string) interface{}
MustGet calls Get underneath will panic if object not found within container
func MustInvoke ¶
func MustInvoke(id string, fn interface{})
MustInvoke calls MustGet underneath will panic if object not found within container
func MustInvokeMany ¶ added in v1.0.2
func MustInvokeMany(ids ...string) func(fn interface{})
MustInvokeMany calls MustInvoke underneath returns many services from container will panic if object not found within container
Types ¶
type Container ¶ added in v1.0.1
type Container interface { Register(id string, object interface{}) Deregister(id string) Has(id string) bool Get(id string) (interface{}, bool) MustGet(id string) interface{} Invoke(id string, fn interface{}) MustInvoke(id string, fn interface{}) MustInvokeMany(ids ...string) func(fn interface{}) }
Container interface