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

Dependency Injection on iOS

Dependency Injection on iOS

Amanjeet Singh

October 20, 2018
Tweet

More Decks by Amanjeet Singh

Other Decks in Programming

Transcript

  1. Dependency
    To inject or not?

    View Slide

  2. 3
    Agenda
    • The problems in your code base
    • The solution (sort of) : Dependency Injection
    • What, why and how of Dependency Injection
    • Introduction to Swinject and Weaver
    • Wrap up

    View Slide

  3. What is the problem
    in your code base?

    View Slide

  4. “Spaghetti Code”
    Spaghetti code phrase for unstructured and difficult to
    maintain source code. It can be caused by volatile project
    requirements, and insufficient ability or experience.

    View Slide

  5. “Unmaintainable Codebase”

    View Slide

  6. “Non testable code”

    View Slide

  7. “Difficulty for fellow peers”

    View Slide

  8. View Slide

  9. Nobody wants that right?
    $

    View Slide

  10. Enter Dependency Injection!

    View Slide

  11. DI what?
    Dependency injection is a technique whereby one
    object supplies the dependencies of another object. An
    injection is the passing of a dependency to a dependent
    object. The service is made part of the client's state.

    View Slide

  12. Fact
    Dependency Injection is a 25 dollar
    term for a 5 cent concept

    View Slide

  13. You’ve already done Dependency Injection,
    unknowingly
    Passing data through Segues, is Dependency
    Injection

    View Slide

  14. What DI is NOT ❌
    • a library
    • a framework
    • a tool

    View Slide

  15. What DI is ✅
    • a way of thinking
    • a way of designing code
    • general guidelines

    View Slide

  16. View Slide

  17. 3
    Current
    •Initialiser Based
    •Property Based
    •Parameter Based

    View Slide

  18. Initialiser Based
    The idea is that an object should be given the
    dependencies it needs when being initialised. The
    benefit is that it guarantees that our objects have
    everything they need in order to work the right
    way.
    This is easy af.

    View Slide

  19. View Slide

  20. Property Based
    Instead of injecting an object’s dependencies in the
    initialiser, properties can simply be assigned
    afterwards. This can also reduce boilerplate code,
    especially when there is stuff that doesn’t need to be
    injected.

    View Slide

  21. View Slide

  22. Parameter Based
    Sometimes we need a specific dependency once,
    or we need to mock it under certain conditions.
    Instead of having to change an object’s init or
    expose properties, we open up an API to accept
    dependency as a parameter.

    View Slide

  23. View Slide

  24. View Slide

  25. Dependency Container

    A Dependency Injection Container (or DI Container) is
    basically an object able to instantiate, retain, and
    resolve other objects’ dependencies for them.

    View Slide

  26. Swinject
    Swinject is a lightweight dependency injection
    framework for Swift. Swinject helps your app split
    loosely-coupled components. Swinject is powered by
    the Swift generic type system and first class functions
    to define dependencies of your app simply.

    View Slide

  27. View Slide

  28. View Slide

  29. View Slide

  30. 3
    Why containers?
    • Inject “N” dependencies with one
    parameter
    • Different init logic for different layers
    • With the right interfaces, Unit tests
    are easy
    • Implements Inversion of control

    View Slide

  31. 3
    Why not Containers?
    • Can crash at runtime
    • Unit tests need their own containers
    • Not easy, very conceptual

    View Slide

  32. Weaver to the Rescue!!

    View Slide

  33. What is Weaver?
    Weaver is a lightweight Dependency Injection
    framework that is able to generate the necessary
    boilerplate code to inject dependencies into Swift types,
    based on annotations.

    View Slide

  34. Quick Wins
    •Works compile time
    •Container Auto Generation
    •Type and Thread safe
    •ObjC Support

    View Slide

  35. 3
    How does it work? ⚒
    • Scans your code for annotations
    • Generates an AST*
    • Generates a Dependency Graph
    • Performs safety checks
    • Generates Boilerplate code using the graph. Generate
    one dependency container/ struct or class with
    injectable dependencies.
    * Abstract Syntax Tree

    View Slide

  36. Implementing a MoviesListController
    3 notice worthy objects
    1) AppDelegate, registering the
    dependencies
    2) MovieManager, providing the movies
    3) MoviesListVC , showing the movies

    View Slide

  37. Registering Dependencies

    View Slide

  38. Defining Scope

    View Slide

  39. Building Root View Controller

    View Slide

  40. MovieManager

    View Slide

  41. View Slide

  42. Further Reading
    .
    1) Medium Article 

    https://medium.com/@JoyceMatos/dependency-injection-in-
    swift-87c748a167be
    2) Podcast by Cocoacasts 

    https://cocoacasts.com/nuts-and-bolts-of-dependency-injection-in-
    swift
    3) Swinject

    https://github.com/Swinject/Swinject
    4) Weaver 

    https://github.com/scribd/Weaver

    View Slide

  43. Amanjeet Singh Bhagat Singh
    @droid_singh @soulful_swift
    Thanks!
    Bobble Keyboard Zomato

    View Slide

  44. Fin.

    View Slide