All Projects → golobby → Container

golobby / Container

Licence: mit
A lightweight yet powerful IoC container for Go projects

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Container

Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+64.38%)
Mutual labels:  ioc, dependency-injection, container, ioc-container
ThunderboltIoc
One of the very first IoC frameworks for .Net that has no reflection. An IoC that casts its services before thunder casts its bolts.
Stars: ✭ 40 (-75%)
Mutual labels:  ioc, dependency-injection, ioc-container
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+651.88%)
Mutual labels:  dependency-injection, ioc, ioc-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (-16.87%)
Mutual labels:  dependency-injection, ioc, ioc-container
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (-60.62%)
Mutual labels:  dependency-injection, ioc, ioc-container
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-81.25%)
Mutual labels:  ioc, dependency-injection, ioc-container
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (-44.37%)
Mutual labels:  dependency-injection, ioc, container
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-96.87%)
Mutual labels:  dependency-injection, ioc, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+166.88%)
Mutual labels:  dependency-injection, ioc, ioc-container
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+85.63%)
Mutual labels:  dependency-injection, ioc, ioc-container
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (-2.5%)
Mutual labels:  ioc, dependency-injection, ioc-container
Node Dependency Injection
The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
Stars: ✭ 140 (-12.5%)
Mutual labels:  dependency-injection, ioc, ioc-container
inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (-69.37%)
Mutual labels:  ioc, dependency-injection, ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (-64.37%)
Mutual labels:  dependency-injection, ioc, ioc-container
SwiftInjection
Dependency Injection framework for Swift
Stars: ✭ 21 (-86.87%)
Mutual labels:  ioc, dependency-injection, ioc-container
alpha-dic
Powerful dependency injection container for node.js
Stars: ✭ 27 (-83.12%)
Mutual labels:  ioc, dependency-injection, ioc-container
iocgo
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang
Stars: ✭ 36 (-77.5%)
Mutual labels:  ioc, dependency-injection, ioc-container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-86.25%)
Mutual labels:  ioc, dependency-injection, ioc-container
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-92.5%)
Mutual labels:  ioc, dependency-injection, ioc-container
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (-15.62%)
Mutual labels:  dependency-injection, ioc, ioc-container

GoDoc Build Status Go Report Card Awesome Coverage Status

Container

A lightweight yet powerful IoC dependency injection container for Go projects. It provides an easy-to-use interface and performance-in-mind dependency injection container to be your ultimate requirement.

Documentation

Required Go Versions

It requires Go v1.11 or newer versions.

Installation

To install this package, run the following command in the root of your project.

go get github.com/golobby/container/v2

Introduction

GoLobby Container like any other IoC dependency injection container is used to bind abstractions to their implementations. Binding is the process of introducing appropriate concretes (implementations) of abstractions to an IoC container. In this process, you also determine the resolving type, singleton or transient. In singleton bindings, the container provides an instance once and returns it for all requests. In transient bindings, the container always returns a brand new instance for each request. After the binding process, you can ask the IoC container to make the appropriate implementation of the abstraction that your code depends on. Now your code depends on abstractions, not implementations.

Quick Start

The following example demonstrates a simple binding and resolving.

// Bind Config (interface) to JsonConfig
err := container.Singleton(func() Config {
    return &JsonConfig{...}
})

var c Config
err := container.Make(&c)
// `c` will be an instance of JsonConfig

Binding

Singleton

Singleton binding using Container:

err := container.Singleton(func() Abstraction {
  return Implementation
})

It takes a resolver function whose return type is the abstraction and the function body returns the concrete (implementation).

Example for singleton binding:

err := container.Singleton(func() Database {
  return &MySQL{}
})

In the example above, the container makes a MySQL instance once and returns it for all requests.

Transient

Transient binding is also similar to singleton binding.

Example for transient binding:

err := container.Transient(func() Shape {
  return &Rectangle{}
})

In the example above, the container always returns a brand new Rectangle instance for each request.

Resolving

Container resolves the dependencies with the method make().

Using References

The easiest way to resolve a dependency is to declare an instance of the abstraction type and pass its reference to Container.

var a Abstraction
err := container.Make(&a)
// `a` will be an implementation of the Abstraction

Example of resolving using refrences:

var m Mailer
err := container.Make(&m)
// `m` will be an implementation of the Mailer interface
m.Send("[email protected]", "Hello Milad!")

Using Closures

The most common way to resolve dependencies is using a function (receiver) with arguments of abstractions you need. The Container will invoke the function and pass the implementations.

err := container.Make(func(a Abstraction) {
    // `a` will be an implementation of the Abstraction
})

Example of resolving using closures:

err := container.Make(func(db Database) {
  // `db` will be an implementation of the Database interface
  db.Query("...")
})

You can also resolve multiple abstractions like tho follwing example:

err := container.Make(func(db Database, s Shape) {
  db.Query("...")
  s.Area()
})

Binding time

You can resolve dependencies at the binding time if you need previous bindings for the new one.

Example of resolving in binding time:

// Bind Config to JsonConfig
err := container.Singleton(func() Config {
    return &JsonConfig{...}
})

// Bind Database to MySQL
err := container.Singleton(func(c Config) Database {
    // `c` will be an instance of `JsonConfig`
    return &MySQL{
        Username: c.Get("DB_USERNAME"),
        Password: c.Get("DB_PASSWORD"),
    }
})

Notice: You can only resolve the dependencies in a binding resolver function that exists in the container.

Standalone instance

In default, the Container keeps your bindings in the global instance. Sometimes you may want to create a standalone instance for a part of your application. If so, create a standalone instance like this example:

c := container.New() // returns a container.Container

err := c.Singleton(func() Database {
    return &MySQL{}
})

err := c.Make(func(db Database) {
    db.Query("...")
})

The rest stays the same. The global container is still available.

Usage Tips

Performance

The package Container inevitably uses reflection in binding and resolving processes. If performance is a concern, you should use this package more carefully. Try to bind and resolve the dependencies out of the processes that are going to run many times (for example, on each request), put it where that run only once when you run your applications like main and init functions.

License

GoLobby Container is released under the MIT License.

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].