container

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2021 License: MIT Imports: 1 Imported by: 91

README

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 a process of introducing an IoC container that which concrete (implementation) is appropriate for an abstraction. In this process, you also determine how it must be resolved, singleton or transient. In singleton binding, the container provides an instance once and returns the same for all requests. In transient binding, 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. In this case, your code depends on abstractions, not implementations.

Binding
Singleton

Singleton binding using Container:

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

It takes a resolver function which its return type is the abstraction and the function body configures the related concrete (implementation) and returns it.

Example for a singleton binding:

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

Transient binding is also similar to singleton binding.

Example for a transient binding:

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

Container resolves the dependencies with the method make().

Using References

One way to get the appropriate implementation you need is to declare an instance of the abstraction type and pass its reference to Container this way:

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

Example:

var m Mailer
err := container.Make(&m)
if err != nil {
    panic(err)
}

m.Send("info@miladrahimi.com", "Hello Milad!")
Using Closures

Another way to resolve the dependencies is by using a function (receiver) that its arguments are the abstractions you need. Container will invoke the function and pass the related implementations for each abstraction.

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

Example:

err := container.Make(func(db Database) {
  // "db" will be the instance of MySQL
  db.Query("...")
})

You can also resolve multiple abstractions this way:

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

You can also resolve a dependency at the binding time in your resolver function like the following example.

// 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 the 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 has already bound.

Standalone instance

Container works without any initialization keeping your bindings in the default instance. Sometimes you may want to create a standalone instance for a part of your application. If so, create a standalone instance:

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

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(receiver interface{}) error

Make will resolve the dependency and return a appropriate concrete of the given abstraction. It can take an abstraction (interface reference) and fill it with the related implementation. It also can takes a function (receiver) with one or more arguments of the abstractions (interfaces) that need to be resolved, Container will invoke the receiver function and pass the related implementations.

func New added in v2.0.1

func New() internal.Container

NewContainer creates a new standalone instance of Container

func Reset

func Reset()

Reset will reset the container and remove all the existing bindings.

func Singleton

func Singleton(resolver interface{}) error

Singleton will bind an abstraction to a concrete for further singleton resolves. It takes a resolver function which returns the concrete and its return type matches the abstraction (interface). The resolver function can have arguments of abstraction that have bound already in Container.

func Transient

func Transient(resolver interface{}) error

Transient will bind an abstraction to a concrete for further transient resolves. It takes a resolver function which returns the concrete and its return type matches the abstraction (interface). The resolver function can have arguments of abstraction that have bound already in Container.

Types

This section is empty.

Directories

Path Synopsis
pkg
container
Package container is a lightweight yet powerful IoC container for Go projects.
Package container is a lightweight yet powerful IoC container for Go projects.

Jump to

Keyboard shortcuts

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