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

DIコンテナを使わないDI / golang.tokyo#11

morikuni
December 11, 2017

DIコンテナを使わないDI / golang.tokyo#11

golang.tokyo#11の発表資料です。

スライド中のリンク
Github: https://github.com/morikuni
Twitter: https://twitter.com/inukirom
goldi: https://github.com/fgrosse/goldi
アドベントカレンダー: https://qiita.com/advent-calendar/2017/go4

morikuni

December 11, 2017
Tweet

More Decks by morikuni

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • Name: ৿ᅳ ହฏ (Morikuni Taihei) • GitHub: @morikuni

    • Twi9er: @inukirom • ॴଐ • גࣜձࣾϝϧΧϦ/ι΢κ΢ • ϝϧΧϦ Χ΢ϧ
  2. ྫ୊ ҎԼͷ3ͭͷؔ਺Λ࢖ͬͯServiceͷΠϯελϯεΛ࡞Εɻ func NewService(repo Repository, mailer Mailer) Service { ...

    } func NewRepository(db *sql.DB) Repository { ... } func NewMailer() Mailer { ... } Service͸RepositoryͱMailerʹґଘ͍ͯ͠Δɻ Repository͸*sql.DBʹґଘ͍ͯ͠Δɻ
  3. mainʹॻ͘ func main() { db, err := sql.Open("db", "dsn") if

    err != nil { ... } repo := NewRepository(db) mailer := NewMailer() service := NewService(repo, mailer) } ΦϒδΣΫτ͕૿͑Δ౓ʹmain͕ංେԽ͍͖ͯ͠Մಡੑ͕௿͍
  4. types: db: package: database/sql type: *DB factory: Open arguments: -

    "db" - "dsn" repository: package: github.com/morikuni/hoge type: Repository factory: NewRepository arguments: - "@db" mailer: package: github.com/morikuni/hoge type: Mailer factory: NewMailer service: package: github.com/morikuni/hoge type: Service factory: NewService arguments: - "@repository" - "@mailer"