summer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2016 License: Apache-2.0 Imports: 3 Imported by: 0

README

summer

The "summer" is a tiny dependency injection and autowiring package for GO language. It was deeply inspired by spring framework and try to implement some tiny portions of its functionality.

About "summer"

Being a beginner in writting something in Golang, I was searching for something similar to spring framework in Java's world ... and I ended up writing my own. So, the "summer" was born.

Go and Java are quite diffent in many ways, and I have no idea whether "Inversion of Control" or "Dependency Injection" is idiomatic Go code should be use or how, so ... mimic other language might be a worse idea.

Installation

To install summer, use go get:

go get github.com/blue-saber/summer
How to Use

Add tag "@Autowired" on Fields (as following example) need a dependency injection. Put "*" as it's value which means it require a Type-based injection, or put a name, "kitty" for example, which means a Name-baed injection.

type Dog struct {
	Icat *ICat   `@Autowired:"kitty"`
	Rabb *Rabbit `@Autowired:"*"`
}

For a Interface pointer or a private field, a proper setter is required to make injection working properly. And the setter is always the first priority to be chosen to inject dependency. Simply put "Set" in front of the field's name as its setter.

func (d *Dog) SetIcat(icat interface{}) {
	if origional, ok := icat.(ICat); ok {
		d.Icat = &origional
	}
}

Like @PostConstruct in spring framework, this package provide a "Summerized" interface and a PostSummerConstruct() function should be implemented if your stuct needed to be called after dependency inject

func (d *Dog) PostSummerConstruct() {
	fmt.Println("Post Construct")
}

The main func look simething like this ...

package main

import (
	"github.com/blue-saber/summer"
)

func main() {
	applicationContext := summer.NewSummer();

	applicationContext.Add(new (sub.Dog), new (sub.Tiger))
	applicationContext.AddWithName("kitty", new (sub.Cat))

	done := applicationContext.Autowiring(func (err bool) {
		if err {
			fmt.Println("Failed to autowiring.")
		} else {
			fmt.Println("Autowired.")

			if result := applicationContext.GetByName("rabbit"); result != nil {
				rabbit := result.(*sub.Rabbit)
				rabbit.Jump()
			}
		}
	});

	err := <-done

	if ! err {
		var icat sub.ICat

		if result := applicationContext.Get(&icat); result != nil {
			icat = result.(sub.ICat)
			icat.Purr()
		}

		var dog *sub.Dog

		if result := applicationContext.Get(dog); result != nil {
			dog = result.(*sub.Dog)
			dog.DoSomething()
		}
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintStruct

func PrintStruct(something interface{})

Types

type Context

type Context struct {
	// contains filtered or unexported fields
}

func NewSummer

func NewSummer() *Context

func (*Context) Add

func (ctx *Context) Add(beans ...interface{})

func (*Context) AddWithName

func (ctx *Context) AddWithName(beanName string, bean interface{}) bool

func (*Context) Autowiring

func (ctx *Context) Autowiring(callback func(err bool)) chan bool

func (*Context) Each

func (ctx *Context) Each(callback func(data interface{})) int

func (*Context) ForEach

func (ctx *Context) ForEach(intf interface{}, callback func(data interface{})) int

func (*Context) Get

func (ctx *Context) Get(intf interface{}) interface{}

func (*Context) GetByName

func (ctx *Context) GetByName(beanName string) interface{}

type Summerized

type Summerized interface {
	PostSummerConstruct()
}

Directories

Path Synopsis
sub

Jump to

Keyboard shortcuts

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