gocontainer

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2019 License: MIT Imports: 3 Imported by: 14

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

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)))
}

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)))
}
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 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 Register

func Register(id string, object interface{})

Register service by id

Types

This section is empty.

Jump to

Keyboard shortcuts

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