Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vardius/gocontainer
Simple Dependency Injection Container
https://github.com/vardius/gocontainer
container-registry dependency-injection go golang
Last synced: 4 months ago
JSON representation
Simple Dependency Injection Container
- Host: GitHub
- URL: https://github.com/vardius/gocontainer
- Owner: vardius
- License: mit
- Created: 2019-06-06T08:18:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-23T09:12:06.000Z (almost 5 years ago)
- Last Synced: 2024-07-31T20:52:21.067Z (6 months ago)
- Topics: container-registry, dependency-injection, go, golang
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 19
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-go - gocontainer - Simple Dependency Injection Container. (Miscellaneous / Dependency Injection)
- zero-alloc-awesome-go - gocontainer - Simple Dependency Injection Container. (Miscellaneous / Dependency Injection)
- awesome-go-extra - gocontainer - 06-06T08:18:07Z|2020-03-23T09:12:06Z| (Microsoft Office / Dependency Injection)
README
πͺ£ gocontainer
================
[![Build Status](https://travis-ci.org/vardius/gocontainer.svg?branch=master)](https://travis-ci.org/vardius/gocontainer)
[![Go Report Card](https://goreportcard.com/badge/github.com/vardius/gocontainer)](https://goreportcard.com/report/github.com/vardius/gocontainer)
[![codecov](https://codecov.io/gh/vardius/gocontainer/branch/master/graph/badge.svg)](https://codecov.io/gh/vardius/gocontainer)
[![](https://godoc.org/github.com/vardius/gocontainer?status.svg)](https://pkg.go.dev/github.com/vardius/gocontainer)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/vardius/gocontainer/blob/master/LICENSE.md)
gocontainer - Dependency Injection Container
π ABOUT
==================================================
Contributors:* [RafaΕ Lorenz](http://rafallorenz.com)
Want to contribute ? Feel free to send pull requests!
Have problems, bugs, feature ideas?
We are using the github [issue tracker](https://github.com/vardius/gocontainer/issues) to manage them.## π Documentation
For __examples__ **visit [godoc#pkg-examples](http://godoc.org/github.com/vardius/gocontainer#pkg-examples)**
For **GoDoc** reference, **visit [pkg.go.dev](https://pkg.go.dev/github.com/vardius/gocontainer)**
[MustInvokeMany](https://godoc.org/github.com/vardius/gocontainer#example-package--MustInvokeMany)
π HOW TO USE
==================================================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
```go
package mainimport (
"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
```go
package databaseimport (
"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
```go
package repositoryimport (
"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.
```go
package mainimport (
"github.com/vardius/gocontainer/example/repository"
"github.com/vardius/gocontainer"
)func main() {
// disable global container instance
gocontainer.GlobalContainer = nilmycontainer := gocontainer.New()
mycontainer.Register("test", 1)
}
```
Please check [GoDoc](http://godoc.org/github.com/vardius/gocontainer) for more methods and examples.π [License](LICENSE.md)
-------This package is released under the MIT license. See the complete license in the package