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

DIライブラリ Airframeを使ってみた

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 DIライブラリ Airframeを使ってみた

Avatar for takeosuzuki

takeosuzuki

April 25, 2019

Other Decks in Programming

Transcript

  1. trait DataBase { def write(text: String): Unit } class RealDB()

    extends DataBase { override def write(text: String): Unit = println(s"$text を本番用DBに書き込みました!") } class TestDB() extends DataBase { override def write(text: String): Unit = println(s"テスト環境のため $text は書き込みしてません!") } trait MyApp { val db = bind[DataBase] } val d = newDesign .bind[DataBase].to[RealDB] d.build[MyApp] { app => app.db.write("お試し") // お試し を本番用DBに書き込みました! // TestDB =>テスト環境のため お試し は書き込みしてません! }
  2. trait MyApp { val firstName = bind[String] val lastName =

    bind[String] } val d = newDesign .bind[String].toInstance("Takeo") .bind[String].toInstance("Suzuki") d.build[MyApp] { app => println(app.firstName) //Takeo println(app.lastName) //Suzuki } d.build[MyApp] { app => println(app.firstName) //Suzuki println(app.lastName) // Suzuki } 予想 結果
  3. trait MyApp { val firstName = bind[String] val lastName =

    bind[String] def f(x: Int): Int } class MyAppImpl extends MyApp { override def f(x: Int): Int = x + 1 } val d = newDesign .bind[String].toInstance("Takeo") .bind[MyApp].to[MyAppImpl] d.build[MyApp] { app => println(app.firstName) // Takeo println(app.lastName) // Takeo println(app.f(10)) //11 } これでいいのか・・・