gocontainer

package module
Version: v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2019 License: MIT Imports: 3 Imported by: 4

README

Vardius - gocontainer

Build Status Go Report Card codecov license

gocontainer - Dependency Injection Container

ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

HOW TO USE

  1. GoDoc

  2. Examples

  3. MustInvokeMany

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.GlobalContainer to nil. This package allows you to create many containers.

package main

import (
    "github.com/vardius/gocontainer/example/repository"
    "github.com/vardius/gocontainer"
)

func main() {
    // disable global container instance
    gocontainer.GlobalContainer = nil

    mycontainer := gocontainer.New()
    mycontainer.Register("test", 1)
}

Please check GoDoc for more methods and examples.

License

This package is released under the MIT license. See the complete license in the package:

LICENSE

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.GlobalContainer to nil. This package allows you to create many containers.

package main

import (
	"github.com/vardius/gocontainer/example/repository"
	"github.com/vardius/gocontainer"
)

func main() {
	// disable global container instance
	gocontainer.GlobalContainer = nil

	mycontainer := gocontainer.New()
	mycontainer.Register("test", 1)
}
Example (Deregister)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	gocontainer.Register("test", 1)
	gocontainer.Deregister("test")

	fmt.Println(gocontainer.Get("test"))
}
Output:

<nil> false
Example (Get)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	gocontainer.Register("test", 1)
	o, ok := gocontainer.Get("test")

	fmt.Println(o)
	fmt.Println(ok)
}
Output:

1
true
Example (Has)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	gocontainer.Register("test", 1)

	fmt.Println(gocontainer.Has("test"))
}
Output:

true
Example (Invoke)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	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"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	gocontainer.Register("test", 1)
	o := gocontainer.MustGet("test")

	fmt.Println(o)
}
Output:

1
Example (MustInvoke)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	gocontainer.Register("test", 1)
	gocontainer.MustInvoke("test", func(i int) {
		fmt.Println(i)
	})

}
Output:

1
Example (MustInvokeMany)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	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"

	"github.com/vardius/gocontainer"
)

func main() {
	// disable global container instance
	gocontainer.GlobalContainer = nil

	mycontainer := gocontainer.New()
	mycontainer.Register("test", 1)

	fmt.Println(mycontainer.MustGet("test"))
}
Output:

1
Example (Register)
package main

import (
	"fmt"

	"github.com/vardius/gocontainer"
)

func main() {
	// called by gocontainer's init() function
	gocontainer.GlobalContainer = gocontainer.New()

	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 Deregister

func Deregister(id string)

Deregister service be id

func Get

func Get(id string) (interface{}, bool)

Get a service by id

func Has

func Has(id string) bool

Has checks if container has an object

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

func Register

func Register(id string, object interface{})

Register service by id

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

var (
	// GlobalContainer global container instance
	// instance initialized by package's init() function
	GlobalContainer Container
)

func New added in v1.0.1

func New() Container

New creates new container

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL