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

簡単高速なDIライブラリToothpick

358791a9c91e7615ffe62c7c6e02e470?s=47 zaki50
January 26, 2018

 簡単高速なDIライブラリToothpick

358791a9c91e7615ffe62c7c6e02e470?s=128

zaki50

January 26, 2018
Tweet

Transcript

  1. ؆୯ߴ଎ͳDIϥΠϒϥϦToothpick DroidKaigi Prelude YAMAZAKI Makoto (@zaki50)

  2. Toothpick https://github.com/stephanenicolas/toothpick

  3. ಛ௃ • γϯϓϧͰ؆୯ • ଎౓͸ͪΐͬͱ͚ͩྼΔ͚Ͳ΄΅ಉ͡ • ͍͔ͭ͘ͷ࢖༻ස౓ͷ௿͍ػೳ͸υϩοϓ Dagger2ͱ͘Β΂ͯ https://github.com/stephanenicolas/toothpick/wiki/Benchmark

  4. ࢖͍ํ • ToothpickΛґଘϥΠϒϥϦͱͯ͠௥Ճ • ϞδϡʔϧΛఆٛ • είʔϓΛఆٛ • είʔϓͰ࢖༻͢ΔϞδϡʔϧΛࢦఆ •

    ஫ೖ
  5. ϥΠϒϥϦ௥Ճ app/build.gradle: dependencies { implementation 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.1.1' kapt 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.1' }

  6. Ϟδϡʔϧఆٛ ApplicationModule.kt class ApplicationModule(private val application: Application) : Module() {

    init { bind() } private fun bind() { val repository = ModelRepository(application) bind(ModelRepository::class.java).toInstance(repository) bind(Foo::class.java).to(FooImpl::class.java) bind(Bar::class.java).toProvider(BarProvider::class.java) } }
  7. ࣮૷Ϋϥε FooImpl.kt class FooImpl @Inject constructor() : Foo { init

    { ... } } BarProvider.kt class BarProvider : Provider<Bar> { override fun get(): Bar = BarImpl() }
  8. είʔϓ

  9. ɹείʔϓͷ࡞Γํ MainActivity.kt class MyApplication : Application() { override fun onCreate()

    { super.onCreate() val scope = Toothpick.openScope(this) scope.installModules(ApplicationModule(this)) } override fun onTerminate() { super.onTerminate() Toothpick.closeScope(this) } }
  10. ɹωετͨ͠είʔϓ MainActivity.kt class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState:

    Bundle?) { super.onCreate(savedInstanceState) val scope = Toothpick.openScopes(application, this) scope.installModules(MainActivityModule(this)) } override fun onDestroy() { super.onDestroy() Toothpick.closeScope(this) } }
  11. ɹগ͠ݡ͍είʔϓ MainActivity.kt class MainActivity : AppCompatActivity() { private lateinit var

    activityId: String override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... activityId = getActivityId(savedInstanceState) scope = Toothpick.openScopes(application, activityId) } private fun getActivityId(savedInstanceState: Bundle?): String { return savedInstanceState?.getString("activity_id") ?: UUID.randomUUID().toString() } }
  12. ɹগ͠ݡ͍είʔϓ(ଓ͖) MainActivity.kt class MainActivity : AppCompatActivity() { ... override fun

    onSaveInstanceState(outState: Bundle?) { super.onSaveInstanceState(outState) outState?.putString("activity_id", activityId) } override fun onDestroy() { super.onDestroy() if (isFinishing) { Toothpick.closeScope(activityId) } } }
  13. Ϟδϡʔϧ࢖༻ MyApplication.kt class MyApplication : Application() { override fun onCreate()

    { super.onCreate() val scope = Toothpick.openScope(this) scope.installModules(ApplicationModule(this)) } override fun onTerminate() { super.onTerminate() Toothpick.closeScope(this) } }
  14. ஫ೖ MainActivity.kt class MainActivity : AppCompatActivity() { @Inject lateinit var

    repo: ModelRepository @Inject lateinit var foo: Foo override fun onCreate() { super.onCreate() val scope = Toothpick.openScopes(application, this) scope.installModules(ApplicationModule(this)) Toothpick.inject(this, scope) } }
  15. γϯάϧτϯ BarImpl.kt @Singleton class BarImpl @Inject constructor() : Bar {

    init { ... } }
  16. Android༻ศརϞδϡʔϧ app/build.gradle: dependencies { implementation 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.1.1' kapt 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.1.1' implementation 'com.github.stephanenicolas.toothpick:smoothie:1.1.1'

    } val scope = Toothpick.openScope(this) scope.installModules(SmoothieApplicationModule(this)) scope.installModules(ApplicationModule(this))
  17. ϦϑϨΫγϣϯͷഉআ app/build.gradle: kapt { generateStubs = true arguments { arg('toothpick_registry_package_name',

    "${android.defaultConfig.applicationId}.toothpick") } }
  18. ϦϑϨΫγϣϯͷഉআ MyApplication.kt ͷ onCreateͰ Toothpick.setConfiguration( Configuration.forProduction().disableReflection()) MemberInjectorRegistryLocator.setRootRegistry( com.example.toothpick.MemberInjectorRegistry()) FactoryRegistryLocator.setRootRegistry( com.example.toothpick.FactoryRegistry())

  19. ·ͱΊ • ͱʹ͔͘Ϟδϡʔϧ΍είʔϓͷఆٛ ͕؆୯ • ύϑΥʔϚϯε΋ྑ͍ • ςετͷαϙʔτ΋͋Δ