All Projects → studyzy → iocgo

studyzy / iocgo

Licence: Apache-2.0 license
A lightweight Inversion of Control (IoC) (Dependency Injection) container for Golang

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to iocgo

Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (+75%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+7430.56%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-38.89%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
Ioc
🦄 lightweight (<1kb) inversion of control javascript library for dependency injection written in typescript
Stars: ✭ 171 (+375%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+269.44%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, 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 (+11.11%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+725%)
Mutual labels:  ioc, dependency-injection, inversion-of-control, ioc-container
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (+97.22%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Di
Dependency Injection and IoC framework for PHP
Stars: ✭ 5 (-86.11%)
Mutual labels:  ioc, dependency-injection, ioc-container
Typedi
Simple yet powerful dependency injection tool for JavaScript and TypeScript.
Stars: ✭ 2,832 (+7766.67%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Ditranquillity
Dependency injection for iOS (Swift)
Stars: ✭ 317 (+780.56%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+3241.67%)
Mutual labels:  ioc, dependency-injection, ioc-container
Qframework
Unity3D System Design Architecture
Stars: ✭ 2,326 (+6361.11%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Go Spring
基于 IoC 的 Go 后端一站式开发框架 🚀
Stars: ✭ 744 (+1966.67%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (+58.33%)
Mutual labels:  ioc, dependency-injection, ioc-container
Typescript Ioc
A Lightweight annotation-based dependency injection container for typescript.
Stars: ✭ 427 (+1086.11%)
Mutual labels:  ioc, dependency-injection, ioc-container
stashbox
A lightweight, fast, and portable dependency injection framework for .NET-based solutions.
Stars: ✭ 120 (+233.33%)
Mutual labels:  ioc, dependency-injection, inversion-of-control
Disco
PSR-11 compatible Dependency Injection Container for PHP.
Stars: ✭ 135 (+275%)
Mutual labels:  ioc, dependency-injection, ioc-container
brisk-ioc
fast light brisk ioc/di container on nodejs; Node下快速 轻量的IoC/DI容器,依赖注入,配合装饰器使用
Stars: ✭ 12 (-66.67%)
Mutual labels:  ioc, dependency-injection, ioc-container
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+344.44%)
Mutual labels:  ioc, dependency-injection, ioc-container

iocgo

A lightweight IoC dependency injection container for Golang Build status Coverage Status

English | 中文

How to use

Installation

it requires Go 1.15 or newer versions. install package:

go get github.com/studyzy/iocgo

Examples

1. Simple

type Fooer interface {
	Foo(int)
}
type Foo struct {
}

func (Foo)Foo(i int)  {
	fmt.Println("foo:",i)
}
type Barer interface {
	Bar(string)
}
type Bar struct {

}
func (Bar) Bar(s string){
	fmt.Println("bar:",s)
}
type Foobarer interface {
	Say(int,string)
}
type Foobar struct {
	foo Fooer
	bar Barer
}
func NewFoobar(f Fooer,b Barer) Foobarer{
	return &Foobar{
		foo: f,
		bar: b,
	}
}
func (f Foobar)Say(i int ,s string)  {
	f.foo.Foo(i)
	f.bar.Bar(s)
}
func TestContainer_SimpleRegister(t *testing.T) {
	container := NewContainer()
	container.Register(NewFoobar)
	container.Register(func() Fooer { return &Foo{} })
	container.Register(func() Barer { return &Bar{} })
	var fb Foobarer
	container.Resolve(&fb)
	fb.Say(123,"Hello World")
}

2. Register options

iocgo support below options when register resolver:

  • Name
  • Optional
  • Interface
  • Lifestyle(isTransient)
  • DependsOn
  • Parameters
  • Default

How to use these options? see test example: container_test.go

3. Register instance

If you already have an instance, you can use :

RegisterInstance(interfacePtr interface{}, instance interface{}, options ...Option) error

b := &Bar{}
var bar Barer //interface
container.RegisterInstance(&bar, b) // register interface -> instance

4. Resolve instance

You can input an interface point to Resolve function, and this function will set correct instance to this interface. if constructor function return an error, this Resolve function also return this error.

var fb Foobarer
err:=container.Resolve(&fb)

Notice: Resolve first parameter is an interface point, not an interface

Resolve function also support options, belows are resolve options:

  • Arguments
  • ResolveName

5. Fill a struct fields

Define a struct include some interface fields, call Fill function can fill all interface fields to instance.

type FoobarInput struct {
	foo Fooer
	bar Barer
	msg string
}
input := FoobarInput{
		msg: "studyzy",
	}
	container.Register(func() Fooer { return &Foo{} })
	container.Register(func() Barer { return &Bar{} })
	err := container.Fill(&input)

struct fields also support below tags:

  • name //Resolve instance by resolver name
  • optional //No instance found, keep nil, not throw error For example:
type FoobarInputWithTag struct {
	foo Fooer `optional:"true"`
	bar Barer `name:"baz"`
	msg string
}

6. Call a function

If you have a function that use interface as parameter, you can direct use Call to invoke this function. For example:

func SayHi1(f Fooer, b Barer) {
	f.Foo(1234)
	b.Bar("hi")
}
Register(func() Fooer { return &Foo{} })
Register(func() Barer { return &Bar{} })
Call(SayHi1)

The same as Resolve function, Call function also support options:

  • CallArguments
  • CallDependsOn

By the way, if invoked function return an error, Call function also return same error. If function return multi values, Call function also return same values as []interface{}

References:

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