• Stars
    star
    18
  • Rank 1,167,781 (Top 24 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Dependency Injection Container

πŸͺ£ gocontainer

Build Status Go Report Card codecov license

logo

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.

πŸ“š Documentation

For examples visit godoc#pkg-examples

For GoDoc reference, visit pkg.go.dev

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

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

More Repositories

1

go-api-boilerplate

Go Server/API boilerplate using best practices DDD CQRS ES gRPC
Go
885
star
2

message-bus

Go simple async message bus
JavaScript
247
star
3

gorouter

Go Server/API micro framework, HTTP request router, multiplexer, mux
Go
151
star
4

gollback

Go asynchronous simple function utilities, for managing execution of closures and callbacks
Go
109
star
5

worker-pool

Go simple async worker pool
Go
87
star
6

peer-data

Library for files, media streaming/sharing using WebRTC
JavaScript
64
star
7

peer-cdn

Lightweight library providing peer to peer CDN functionality
JavaScript
61
star
8

progress-go

Go simple progress bar writing to output
Go
53
star
9

web-components-webpack-es6-boilerplate

Web Components project starter using ES6 and Webpack
JavaScript
44
star
10

pubsub

gRPC message-oriented middleware on top of message-bus, event ingestion and delivery system.
Go
41
star
11

react-webrtc-chat

React WebRTC chat
JavaScript
41
star
12

web-component

Lightweight library providing interface for building web components
JavaScript
39
star
13

webrtc-chat

Serverless chat application useing peer to peer WebRTC
JavaScript
29
star
14

react-peer-data

React wrapper for PeerData library for files, media streaming/sharing using WebRTC.
TypeScript
20
star
15

react-user-media

React wrapper for getUserMedia
TypeScript
19
star
16

shutdown

Simple go signals handler for performing graceful shutdown by executing callback function
Go
18
star
17

pushpull

gRPC message-oriented middleware on top of worker-pool, event ingestion and delivery system.
Go
10
star
18

peer-data-server

Signaling server, messaging service on Node using socket
JavaScript
10
star
19

invoice-bundle

Symfony invoice bundle
JavaScript
8
star
20

blockchain

Simple gRPC blockchain
Go
8
star
21

crud-bundle

Provides crud actions, crud bundle for Symfony
PHP
7
star
22

trace

Simple helper to trace the function calls, errors or logs reference
Go
7
star
23

list-bundle

Provides list builder, list view bundle for Symfony
PHP
4
star
24

golog

Go logger
Go
4
star
25

goquery

Go query builder for sql
Go
3
star
26

gocrud

Simple Go (Golang) CRUD provider
Go
3
star
27

angular-symfony-acl

ACL component for Angular Js based on symfony2 user roles
JavaScript
2
star
28

angular-gravatar

Angular gravatar component
JavaScript
2
star
29

mean-todos

MEAN (MongoDB, ExpressJS, Angular2, NodeJS) Todo List application
TypeScript
1
star
30

ng2-search

Angular 2 search module
JavaScript
1
star
31

angular2-chat

Socket.io Chat with NodeJS and Angular2
JavaScript
1
star
32

user-bundle

Simple symfony doctrine user bundle
PHP
1
star
33

ng2-pagination

Angular2 pagination module
TypeScript
1
star
34

angular-oauth2

Angular oauth2 provider
JavaScript
1
star
35

vardius.github.io

SCSS
1
star
36

angular2-github

GitHub User Search - Angular2 + Webpack App
JavaScript
1
star
37

menu-bundle

Simple symfony menu builder
PHP
1
star
38

angular2-spotify

Spotify - Angular2 + Webpack App
JavaScript
1
star
39

lru-cache

A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn't been used for the longest amount of time.
Go
1
star
40

admin-bundle

Symfony admin bundle, CMS
JavaScript
1
star