All Projects → vardius → gocontainer

vardius / gocontainer

Licence: MIT license
Simple Dependency Injection Container

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to gocontainer

Pimple
A small PHP dependency injection container
Stars: ✭ 2,491 (+13738.89%)
Mutual labels:  dependency-injection
Grace
Grace is a feature rich dependency injection container library
Stars: ✭ 240 (+1233.33%)
Mutual labels:  dependency-injection
hypo-container
A dependency injection container.
Stars: ✭ 16 (-11.11%)
Mutual labels:  dependency-injection
Angular Awesome List
Список ресурсов по Angular на русском
Stars: ✭ 224 (+1144.44%)
Mutual labels:  dependency-injection
Ulfberht
🗡️ A small but powerful & opinionated DI library. Written in Kotlin, and powered by annotation processing.
Stars: ✭ 234 (+1200%)
Mutual labels:  dependency-injection
Typhoon Example
An example application built with Typhoon dependency injection framework.
Stars: ✭ 246 (+1266.67%)
Mutual labels:  dependency-injection
Inversify Express Example
The official express + inversify+ inversify-express-utils examples
Stars: ✭ 210 (+1066.67%)
Mutual labels:  dependency-injection
StartupModules
Startup modules for ASP.NET Core.
Stars: ✭ 33 (+83.33%)
Mutual labels:  dependency-injection
Grafter
Grafter is a library to configure and wire Scala applications
Stars: ✭ 240 (+1233.33%)
Mutual labels:  dependency-injection
DI-compiler
A Custom Transformer for Typescript that enables compile-time Dependency Injection
Stars: ✭ 62 (+244.44%)
Mutual labels:  dependency-injection
Transfuse
💉 Transfuse - A Dependency Injection and Integration framework for Google Android
Stars: ✭ 226 (+1155.56%)
Mutual labels:  dependency-injection
Invoker
Generic and extensible callable invoker
Stars: ✭ 229 (+1172.22%)
Mutual labels:  dependency-injection
Kotlin Inject
Dependency injection lib for kotlin
Stars: ✭ 248 (+1277.78%)
Mutual labels:  dependency-injection
Coldbox Platform
A modern, fluent and conventions based HMVC framework for ColdFusion (CFML)
Stars: ✭ 220 (+1122.22%)
Mutual labels:  dependency-injection
alice
An additive dependency injection container for Golang.
Stars: ✭ 51 (+183.33%)
Mutual labels:  dependency-injection
Swinjectstoryboard
Swinject extension for automatic dependency injection via Storyboard
Stars: ✭ 211 (+1072.22%)
Mutual labels:  dependency-injection
Movies Kotlin Kata
Katas for practice Kotlin( Coroutines, dataclasses, delegate properties...) Clean Architecture and best practices in Android(DI, Dagger, MVP, Espresso) implemented by Jorge Sánchez (Xurxodev)
Stars: ✭ 240 (+1233.33%)
Mutual labels:  dependency-injection
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+566.67%)
Mutual labels:  dependency-injection
gcloud-login-action
A Github Action which can be used to authenticate with Google Cloud Container Registry
Stars: ✭ 21 (+16.67%)
Mutual labels:  container-registry
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+14961.11%)
Mutual labels:  dependency-injection

🪣 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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].