Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection in Go

Browny Lin
August 05, 2017

Dependency Injection in Go

This is the slide for the talk I shared in coscup2017. The video recording is here: https://youtu.be/4EWAatxGS6Q?t=7h8m

Introduction to some basic concepts: Dependency Inversion Principle (DIP), Inversion of Control (IoC), Dependency Injection (DI). Demo a golang injection framework and review the pros and cons about DI.

Browny Lin

August 05, 2017
Tweet

More Decks by Browny Lin

Other Decks in Programming

Transcript

  1. Dependency Inversion Principle (DIP) • S.O.L.I.D. • A guiding principle

    to loosely coupled system ◦ High-level modules should not depend on low-level modules. Both should depend on abstractions ◦ Abstractions should not depend on details. Details should depend on abstractions
  2. Dependency Inversion Principle (DIP) • S.O.L.I.D. • A guiding principle

    to loosely coupled system ◦ High-level modules should not depend on low-level modules. Both should depend on abstractions ◦ Abstractions should not depend on details. Details should depend on abstractions
  3. Dependency Inversion Principle (DIP) • S.O.L.I.D. • A guiding principle

    to loosely coupled system ◦ Abstraction & Inversion
  4. func (e *Encryptor) Run(src, dst string) error { dat, err

    := ioutil.ReadFile(src) if err != nil { return nil } result := e.encrypt(dat) return ioutil.WriteFile(dst, result, 0644) } func (e *Encryptor) encrypt(dat []byte) []byte { return []byte("awesome encrypt") } read src from file encrypt write result to dst
  5. func (e *Encryptor) Run(srcType, dstType string) error { var src

    []byte switch srcType { case "file": src = e.readFromFile() case "database": src = e.readFromDatabase() } // encrypt r := e.encrypt(dat) switch dstType { case "file": src = e.writeToFile(r) case "webservice": src = e.writeToWebservice(r) } } read src according to srcType write result according to dstType
  6. func (e *Encryptor) Run(srcType, dstType string) error { var src

    []byte switch srcType { case "file": src = e.readFromFile(x, y, z) case "database": src = e.readFromDatabase(i, j) } // encrypt ... } Depends on low level module interface
  7. func (e *Encryptor) Run( srcType, dstType, x, y, z, i,

    j string) error { var src []byte switch srcType { case "file": src = e.readFromFile(x, y, z) case "database": src = e.readFromDatabase(i, j) } // encrypt ... } 1. Changes are risky 2. Testing is difficult 3. Semantics is complex
  8. func (e *Encryptor) Run(r IReader, w IWriter) error { //

    read file dat, err := r.Read() if err != nil { return nil } // encrypt result := e.encrypt(dat) // output encrypted content return w.Write(result) } High level defines the abstraction type IReader interface { Read() ([]byte, error) } type IWriter interface { Write(dat []byte) error }
  9. type fileReader struct { src string } func (f *fileReader)

    Read() ([]byte, error) { return ioutil.ReadFile(f.src) } type dbReader struct { host string query string } func (d *dbReader) Read() ([]byte, error) { return []byte("query db: host[%s], query[%s]", d.host, d.query) } Low level implements the abstraction type IReader interface { Read() ([]byte, error) }
  10. fr := &fileReader{ src: "/a/b/c", } dbr := &dbReader{ host:

    "127.0.0.1", query: "q", } e.Run(fr, ...) or e.Run(dbr, ...) 1. Changes are NOT risky 2. Testing is NOT difficult 3. Semantics is NOT complex
  11. • DIP in different scopes ◦ The control of the

    interface ◦ The control of dependency creation and binding ◦ The control of the flow (procedural to event-driven) Inversion of Control Dependency Injection
  12. Dependency Injection A dependency is passed to an object as

    an argument rather than the object creating or finding it inversion
  13. fr := &fileReader{ src: "/a/b/c", } dbr := &dbReader{ host:

    "127.0.0.1", query: "q", } e.Run(fr, ...) Some kinds of injection
  14. Dependency Inversion Principle Inversion of Control Dependency Injection Abstraction &

    Inversion applied on different design scopes to address the problems of coupled system
  15. Problems (nested/meshed dependencies) Foo Bar IBar Bar Woo IWoo Woo

    woo := &Woo{} bar := &Bar{Woo: woo} foo := &Foo{Bar: bar}
  16. package example type Logger interface { Log(format string, a ...interface{})

    } type Food interface { GetRice() } type Machine interface { Run(n int) error } type Transport interface { Fly(src, dst string) }
  17. type MyLogger struct{} func (m *MyLogger) Log(format string, v ...interface{})

    { log.Printf(format, v...) } type Master struct { Logger `inject:"logger"` Food `inject:"example.Master.Food"` Transport `inject:"example.Master.Transport"` } Mailbox address of dependencies
  18. type Farmer struct { Logger `inject:"logger"` Machine `inject:"example.TillageMachine.Machine"` } func

    (f *Farmer) GetRice() { err := f.Machine.Run(3) if err != nil { f.Log("Machine breaks, no rice") } f.Log("Got rice") } type TillageMachine struct { Logger `inject:"logger"` } func (tm *TillageMachine) Run(n int) error { tm.Log("Tillage %d hours", n) return nil }
  19. type Driver struct { Logger `inject:"logger"` plane string } func

    (d *Driver) Setup() error { d.plane = "Boeing787" return nil } func (d *Driver) Fly(src, dst string) { d.Log("%s Fly from %s to %s", d.plane, src, dst) } Constructor }
  20. depMap := map[interface{}][]string{ &myLogger: []string{ "logger", }, &driver: []string{ "example.Master.Transport",

    }, &farmer: []string{ "example.Master.Food", }, &tillMachine: []string{ "example.TillageMachine.Machine", }, &master: []string{}, } driver := example.Driver{} farmer := example.Farmer{} master := example.Master{} myLogger := example.MyLogger{} tillMachine := example.TillageMachine{}
  21. graph, err := Weave(depMap) s.NoError(err) master.Food.GetRice() master.Transport.Fly("C++", "Go") f :=

    graph[reflect.TypeOf(&example.Farmer{})].(*example.Farmer) f.Machine.Run(5)
  22. Disadvantages • DI framework dependent • Code is difficult to

    trace • Errors are pushed to run-time (circular reference, bad binding, ...)
  23. Caveats • DI framework dependent -> 凡事總有代價 • Code is

    difficult to trace -> 好的風格 • Errors are pushed to run-time -> 想辦法測試
  24. • Clarify terms (DIP, IoC, DI) • Go through a

    DI framework • Review the disadvantages Recap