Code Monkey home page Code Monkey logo

go-ioc's Introduction

Golang 依赖注入

为什么我们需要依赖注入:

Most important, for me, is making it easy to follow the Single Responsibility Principle.

DI/IoC makes it simple for me to manage dependencies between objects. 
In turn, that makes it easier for me to break coherent functionality off into it's 
own contract (interface). 

As a result, my code has been far more modularized since I learned of DI/IoC.
Another result of this is that I can much more easily see my way through to a 
design that supports the Open-Closed Principle. This is one of the most confidence 
inspiring techniques (second only to automated testing). I doubt I could espouse the 
virtues of Open-Closed Principle enough.

DI/IoC is one of the few things in my programming career that has been a "game changer." 
There is a huge gap in quality between code I wrote before & after learning DI/IoC. 
Let me emphasize that some more. HUGE improvement in code quality.

安装

go get github.com/douyacun/go-ioc

用法

构造一个问候服务需要知道

  1. 向谁问候
  2. 问候什么
package simple

import (
	"fmt"
	di "github.com/douyacun/go-ioc"
	"testing"
)

type MessagePrinter interface {
	Print()
}

type UserProvider interface {
	GetUserName() string
}

type UserProviderImpl struct {
	UserName string `di:"userName"` // will match message
}

func (impl *UserProviderImpl) GetUserName() string {
	return impl.UserName
}

type MessagePrinterImpl struct {
	message string `di:"message"` // will match message
}

func (impl *MessagePrinterImpl) Print() {
	fmt.Println(impl.message)
}

func (impl *MessagePrinterImpl) SetMessage(m string) {
	impl.message = m
}

type GreeterService struct {
	UserProvider UserProvider   `di:"*"` // will match type
	Printer      MessagePrinter `di:"*"` // will match type
}

func (s *GreeterService) Print() {
	fmt.Printf("%s:", s.UserProvider.GetUserName())
	s.Printer.Print()
}

通常来说的初始化过程

message := "say hello"
printer := &MessagePrinterImpl{}
printer.SetMessage(message)

userName := "word"
userProvider := &UserProviderImpl{UserName: userName}

service := &MessagePrinterService{Printer: printer, UserProvider:userProvider}
service.Print()

使用依赖注入后

di.Register("message", "hello world")
di.Register("userName", "douyacun")
di.Register("printer", &MessagePrinterImpl{})
di.Register("userProvider", &UserProviderImpl{})

service := &GreeterService{}

di.MustBind(service)
service.Print()

go-ioc's People

Contributors

douyacun avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.