container

package module
v1.0.1 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

Build Status

Container

An IoC Container written in Go. 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(function interface{})

Make will resolve the given abstraction and return related concrete It takes a function with one argument of the abstraction type, the Container invokes the function an pass the related create

func Singleton

func Singleton(function interface{})

Singleton will bind an abstraction to a singleton concrete It takes a resolver function which returns the concrete and its return type matches the abstraction

func Transient

func Transient(function interface{})

Transient will bind an abstraction to a transient concrete It takes a resolver function which returns the concrete and its return type matches the abstraction

Types

This section is empty.

Jump to

Keyboard shortcuts

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