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))) }
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 (Register) ¶
package main import ( "fmt" gocontainer "github.com/vardius/gocontainer" ) func main() { gocontainer.Register("test", 1) fmt.Println(gocontainer.MustGet("test")) }
Output: 1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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
Types ¶
This section is empty.