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

Framework-Free Dependency Injection (360|AnDev ...

Framework-Free Dependency Injection (360|AnDev 2019)

Dependency injection is an important technique that helps us write testable Android apps. For large apps with complex requirements, powerful frameworks like Dagger centralize and organize our dependency relationships and lifecycles and automate injection. However, many applications are simple enough that manual dependency injection provides all the benefits of Dagger with few of the costs (e.g. a steep learning curve; slower build times). In this beginner-friendly talk, we’ll learn how to identify, extract, and inject dependencies by hand in several common Android architectures. Attendees will leave with a solid grasp of dependency injection fundamentals and the ability (and desire!) to introduce manual dependency injection into their own codebases.

Stuart Kent

July 18, 2019
Tweet

More Decks by Stuart Kent

Other Decks in Technology

Transcript

  1. 2019 ! Dagger vs Koin Renewed focus on actual DI

    needs More interest in simple DI solutions
  2. This Talk Unit test driven dependency injection Simple method; not

    scary ⚖ Advantages, limitations, options
  3. This code works great in production. class HumanTimeHelper { //

    Consumer fun getTimeOfDay(): String { return when (LocalTime.now().hour) { // Dependency in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  4. This code works great in production. class HumanTimeHelper { //

    Consumer fun getTimeOfDay(): String { return when (LocalTime.now().hour) { // Dependency in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  5. This code works great in production. class HumanTimeHelper { //

    Consumer fun getTimeOfDay(): String { return when (LocalTime.now().hour) { // Dependency in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  6. This code works great in production. class HumanTimeHelper { //

    Consumer fun getTimeOfDay(): String { return when (LocalTime.now().hour) { // Dependency in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  7. Unit testing HumanTimeHelper is... impossible? @Test fun testGetTimeOfDayMorning() { val

    humanTimeHelper = HumanTimeHelper() val expected = "Morning" val actual = humanTimeHelper.getTimeOfDay() // Will fail ~70% of the time! assertEquals(expected, actual) }
  8. Our consumer (HumanTimeHelper) is tightly coupled to its dependency (LocalTime.now()).

    class HumanTimeHelper { fun getTimeOfDay(): String { return when (LocalTime.now().hour) { in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  9. Goals Specify a fake time in unit tests Continue to

    use the real time in production
  10. Step 1: define an interface that describes the ideal behavior

    of the dependency from the point of view of the consumer. interface IClock { fun hour(): Int }
  11. Step 2: inject an instance of the new interface into

    the consumer via its constructor(s), and save it as a property. class HumanTimeHelper(private val clock: IClock) { fun getTimeOfDay(): String { return when (LocalTime.now().hour) { in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  12. Step 3: replace the tightly-coupled dependency with the newly- injected

    instance throughout the consumer's code. class HumanTimeHelper(private val clock: IClock) { fun getTimeOfDay(): String { return when (clock.hour()) { in 6..12 -> "Morning" in 13..17 -> "Afternoon" in 18..21 -> "Evening" else -> "Night" } } }
  13. Step 4: create a production implementation of the new interface

    that mimics original behavior. class SystemClock : IClock { override fun hour() = LocalTime.now().hour }
  14. Step 4: create a production implementation of the new interface

    that mimics original behavior. class SystemClock : IClock { override fun hour() = LocalTime.now().hour }
  15. Step 5: update all consumer constructor calls in production code,

    passing in the production implementation. val humanTimeHelper = HumanTimeHelper(SystemClock())
  16. Step 6: update all consumer constructor calls in test code,

    passing in a mock implementation with deterministic behavior. @Test fun testGetTimeOfDayMorning() { val humanTimeHelper = HumanTimeHelper(object : IClock { override fun hour() = 6 }) val expected = "Morning" val actual = humanTimeHelper.getTimeOfDay() // Will pass 100% of the time! assertEquals(expected, actual) }
  17. Step 6: update all consumer constructor calls in test code,

    passing in a mock implementation with deterministic behavior. @Test fun testGetTimeOfDayMorning() { val humanTimeHelper = HumanTimeHelper(object : IClock { override fun hour() = 6 }) val expected = "Morning" val actual = humanTimeHelper.getTimeOfDay() // Will pass 100% of the time! assertEquals(expected, actual) }
  18. Step 6: update all consumer constructor calls in test code,

    passing in a mock implementation with deterministic behavior. @Test fun testGetTimeOfDayEvening() { val humanTimeHelper = HumanTimeHelper(object : IClock { override fun hour() = 19 }) val expected = "Evening" val actual = humanTimeHelper.getTimeOfDay() // Will pass 100% of the time! assertEquals(expected, actual) }
  19. Recipe Recap 1. Create ideal interface. 2. Inject interface into

    constructor. 3. Use injected interface. 4. Create real implementation.
  20. Recipe Recap 1. Create ideal interface. 2. Inject interface into

    constructor. 3. Use injected interface. 4. Create real implementation. 5. Pass real implementation in production.
  21. Recipe Recap 1. Create ideal interface. 2. Inject interface into

    constructor. 3. Use injected interface. 4. Create real implementation. 5. Pass real implementation in production. 6. Pass mock implementation in tests.
  22. Android MVP We've dealt with a dependency affected by time.

    Next we'll practice our recipe with dependencies affected by: • network conditions • local storage state We'll start with MVP as it's slightly simpler.
  23. class CreditCardPresenter(private val view: ICreditCardsView, context: Context) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() view.advance() } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) RestApi() .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) } }
  24. class CreditCardPresenter(private val view: ICreditCardsView, context: Context) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() view.advance() } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) RestApi() .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) } }
  25. class CreditCardPresenter(private val view: ICreditCardsView, context: Context) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() view.advance() } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) RestApi() .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) } }
  26. class CreditCardPresenter(private val view: ICreditCardsView, context: Context) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() view.advance() } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) RestApi() .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) } }
  27. class CreditCardsActivity : AppCompatActivity(), ICreditCardsView { private lateinit var presenter:

    CreditCardPresenter override fun onCreate(savedInstanceState: Bundle?) { // ... presenter = CreditCardPresenter(this, this) } // ... }
  28. class CreditCardPresenter(private val view: ICreditCardsView, context: Context) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() view.advance() } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) RestApi() .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) } }
  29. Step 2: Inject interface into constructor. class CreditCardPresenter( private val

    view: ICreditCardsView, private val userCardsFetcher: IUserCardsFetcher, context: Context ) { // ... }
  30. Step 3: Use injected interface. fun refreshCards() { val lastCardId

    = prefs.getInt("lastCardId", -1) userCardsFetcher .fetchUserCards() .flatMapIterable { it } .map { it.copy(favorite = (it.id == lastCardId)) } .toList() .subscribe(Consumer { view.display(it) }) }
  31. Step 4: Create Update real implementation. class RestApi : IUserCardsFetcher

    { // ... override fun fetchUserCards(): Observable<List<CreditCard>> { // ... } // ... }
  32. Step 5: Pass real implementation in production. class CreditCardsActivity :

    AppCompatActivity(), ICreditCardsView { private lateinit var presenter: CreditCardPresenter override fun onCreate(savedInstanceState: Bundle?) { // ... presenter = CreditCardPresenter(this, RestApi(), this) } // ... }
  33. Step 6: Pass mock implementation in tests. ⚠ We're not

    ready to test yet; both presenter methods still use a hard-coded dependency!
  34. class CreditCardPresenter( // ... context: Context ) { private val

    prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) fun onCardSelected(card: CreditCard) { prefs.edit().putInt("lastCardId", card.id).apply() // ... } fun refreshCards() { val lastCardId = prefs.getInt("lastCardId", -1) // ... } }
  35. Step 2: Inject interface into constructor and remove injected Context.

    class CreditCardPresenter( private val view: ICreditCardsView, private val userCardsFetcher: IUserCardsFetcher, private val lastCardStorage: ILastCardStorage ) { // ... }
  36. Step 3: Use injected interface and remove SharedPreferences property. fun

    onCardSelected(card: CreditCard) { lastCardStorage.saveLastCardId(card.id) view.advance() } fun refreshCards() { val lastCardId = lastCardStorage.getLastCardId() // ... }
  37. Step 4: Create real implementation. class PrefsLastCardStorage(context: Context) : ILastCardStorage

    { private val prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) override fun getLastCardId(): Int? { val lastCardId = prefs.getInt("lastCardId", -1) return if (lastCardId >= 0) lastCardId else null } override fun saveLastCardId(id: Int) { prefs.edit().putInt("lastCardId", id).apply() } }
  38. Step 4: Create real implementation. class PrefsLastCardStorage(context: Context) : ILastCardStorage

    { private val prefs = context.getSharedPreferences("creditCard", MODE_PRIVATE) override fun getLastCardId(): Int? { val lastCardId = prefs.getInt("lastCardId", -1) return if (lastCardId >= 0) lastCardId else null } override fun saveLastCardId(id: Int) { prefs.edit().putInt("lastCardId", id).apply() } }
  39. Step 5: Pass real implementation in production. class CreditCardsActivity :

    AppCompatActivity(), ICreditCardsView { private lateinit var presenter: CreditCardPresenter override fun onCreate(savedInstanceState: Bundle?) { // ... presenter = CreditCardPresenter( this, RestApi(), PrefsLastCardStorage(this) ) } // ... }
  40. Step 6: Pass mock implementations in tests. @Test fun `correct

    card marked favorite`() { whenever(mockFetcher.fetchUserCards()).thenReturn( Observable.just(listOf( CreditCard(id = 1, lastFour = "1234", favorite = false), CreditCard(id = 2, lastFour = "7529", favorite = false) )) ) whenever(mockStorage.getLastCardId()).thenReturn(2) val presenter = CreditCardPresenter(mockView, mockFetcher, mockStorage) presenter.refreshCards() verify(mockView).display(cardsCaptor.capture()) val favoriteIds = cardsCaptor.firstValue.filter(CreditCard::favorite) assertEquals(favoriteIds.size, 1) assertEquals(favoriteIds.first().id, 2) }
  41. Step 6: Pass mock implementations in tests. @Test fun `correct

    card marked favorite`() { whenever(mockFetcher.fetchUserCards()).thenReturn( Observable.just(listOf( CreditCard(id = 1, lastFour = "1234", favorite = false), CreditCard(id = 2, lastFour = "7529", favorite = false) )) ) whenever(mockStorage.getLastCardId()).thenReturn(2) val presenter = CreditCardPresenter(mockView, mockFetcher, mockStorage) presenter.refreshCards() verify(mockView).display(cardsCaptor.capture()) val favoriteIds = cardsCaptor.firstValue.filter(CreditCard::favorite) assertEquals(favoriteIds.size, 1) assertEquals(favoriteIds.first().id, 2) }
  42. Step 6: Pass mock implementations in tests. @Test fun `correct

    card marked favorite`() { whenever(mockFetcher.fetchUserCards()).thenReturn( Observable.just(listOf( CreditCard(id = 1, lastFour = "1234", favorite = false), CreditCard(id = 2, lastFour = "7529", favorite = false) )) ) whenever(mockStorage.getLastCardId()).thenReturn(2) val presenter = CreditCardPresenter(mockView, mockFetcher, mockStorage) presenter.refreshCards() verify(mockView).display(cardsCaptor.capture()) val favoriteIds = cardsCaptor.firstValue.filter(CreditCard::favorite) assertEquals(favoriteIds.size, 1) assertEquals(favoriteIds.first().id, 2) }
  43. Step 6: Pass mock implementations in tests. @Test fun `correct

    card marked favorite`() { whenever(mockFetcher.fetchUserCards()).thenReturn( Observable.just(listOf( CreditCard(id = 1, lastFour = "1234", favorite = false), CreditCard(id = 2, lastFour = "7529", favorite = false) )) ) whenever(mockStorage.getLastCardId()).thenReturn(2) val presenter = CreditCardPresenter(mockView, mockFetcher, mockStorage) presenter.refreshCards() verify(mockView).display(cardsCaptor.capture()) val favoriteIds = cardsCaptor.firstValue.filter(CreditCard::favorite) assertEquals(favoriteIds.size, 1) assertEquals(favoriteIds.first().id, 2) }
  44. Android MVVM This recipe also works for MVVM... with tweaks!

    • Construction of Android view models is controlled by a ViewModelFactory. • We'll need a custom factory to inject our dependencies.
  45. class CreditCardsActivity : AppCompatActivity() { // ... override fun onCreate(savedInstanceState:

    Bundle?) { // ... viewModel = ViewModelProviders.of(this) .get(CreditCardsViewModel::class.java) } }
  46. class CreditCardsVMF( private val context: Context ) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(vm: Class<T>): T { return CreditCardsViewModel( RestApi(), PrefsLastCardStorage(context) ) as T } }
  47. class CreditCardsVMF( private val context: Context ) : ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(vm: Class<T>): T { return CreditCardsViewModel( RestApi(), PrefsLastCardStorage(context) ) as T } }
  48. class CreditCardsActivity : AppCompatActivity() { // ... override fun onCreate(savedInstanceState:

    Bundle?) { // ... viewModel = ViewModelProviders.of(this, CreditCardsVMF(this)) .get(CreditCardsViewModel::class.java) } }
  49. Recipe Strengths • Very explicit. • Short learning curve. •

    No build or run time performance impact. • Consumers decoupled from dependency implementations.
  50. Recipe Strengths • Very explicit. • Short learning curve. •

    No build or run time performance impact. • Consumers decoupled from dependency implementations. • Consumers ignorant of dependency lifecycles.
  51. Recipe Strengths • Very explicit. • Short learning curve. •

    No build or run time performance impact. • Consumers decoupled from dependency implementations. • Consumers ignorant of dependency lifecycles. • Possible to adopt incrementally.
  52. Potential Recipe Weaknesses • Your needs dictate whether these are

    important. • Frameworks (Dagger, Koin) solve all of them, but not for free! • Let's identify and evaluate.
  53. Boilerplate Explosion • Constructor injection adds code at declaration and

    call sites. • Evaluation: • Explicit is good. • Proximity is good. • Checked by compiler (unlike e.g. Parcelable). • Easy if each interface has 1 production implementation.
  54. Interface Explosion • Consumer-centric interfaces add code & cognitive load.

    • Evaluation: • Solve by balancing cohesion against interface segregation principle when designing interfaces. • e.g. prefer IRestApi to IUserCardsFetcher.
  55. Not DRY • No centralized control of dependency relationships. •

    Evaluation: • Problematic for UI tests. • Hard to swap production implementations at runtime. • Framework-free solutions exist; see "DIY Dependency Injection with Kotlin" talk by Sam Edwards.