container

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2019 License: MIT Imports: 1 Imported by: 21

README

GoDoc Build Status Coverage State

Container

An IoC Container for Go projects. It provides simple, fluent and easy-to-use APIs to make dependency injection in GoLang very easier.

Documentation

Supported Versions

It requires Go v1.11 or newer versions.

Installation

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

go get github.com/golobby/container
Binding

To bind an abstraction to a concrete for further singleton resolutions:

container.Singleton(func() Abstraction {
  return Implementation
})

It invokes the resolver function once and always return the same object each time you call make() method.

And to bind an abstraction to a concrete for further transient resolutions:

container.Transient(func() Abstraction {
  return Implementation
})

It invokes the resolver function to provide a brand new object each time you call make() method.

Take a look at examples below:

Singleton example:

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

Transient example:

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

To make a concrete by its abstraction:

container.Make(func(a Abstraction) {
  // a will be a concrete of Abstraction
})

For example:

container.Make(func(db Database) {
  // db is an instance of MySQL
  db.Query("...")
})

You can resolve multiple abstractions:

container.Make(func(db Database, s Shape) {
  // db is an instance of MySQL
  // s is an instance of Rectangle
  db.Query("...")
  s.Area()
})

License

GoLobby Container is initially created by @miladrahimi and @amirrezaask, and released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(receiverFunction interface{})

Make will resolve the dependency and return the concrete of given abstraction. It takes a function (receiver) with one or more arguments of the abstractions (interfaces) that need to be resolved, the Container invokes the receiver function and pass the related concretes.

func Singleton

func Singleton(resolverFunction interface{})

Singleton will bind an abstraction to a concrete for further singleton resolutions. It takes a resolver function which returns the concrete and its return type matches the abstraction (interface).

func Transient

func Transient(resolverFunction interface{})

Transient will bind an abstraction to a concrete for further transient resolutions. It takes a resolver function which returns the concrete and its return type matches the abstraction (interface).

Types

This section is empty.

Jump to

Keyboard shortcuts

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