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

Dependency Injection in Practice (iOSCon)

Dependency Injection in Practice (iOSCon)

Yoichi Tagaya

March 31, 2017
Tweet

More Decks by Yoichi Tagaya

Other Decks in Programming

Transcript

  1. !ZPJDIJUHZ Yoichi Tagaya w J04&OHJOFFSBU w $$.BSLFUQMBDFBQQ  "WBJMBCMFJO 

    .%PXOMPBETJOUPUBM w )JSJOHJO  4BO'SBODJTDP  -POEPO  5PLZP ! " # !"#
  2. !ZPJDIJUHZ An Author of Swinject • Dependency Injection framework for

    Swift • Maintained by 5 members • 1.4k+ stars on GitHub • Supporting iOS, macOS, tvOS, watchOS and Linux • 4 extensions provided • github.com/Swinject/Swinject
  3. !ZPJDIJUHZ Dependency Injection in Practice 1. Basic Concepts 2. Dependency

    Injection Container 3. Cake Pattern 5PEBZT5BML
  4. !ZPJDIJUHZ What's Dependency Injection Inversion of Control for resolving dependencies

    You create what you use. ↺ What you use are created and passed.
  5. !ZPJDIJUHZ What's Dependency Injection Inversion of Control for resolving dependencies

    You create what you use. ↺ What you use are created and passed. - Initializer Injection (Constructor Injection) - Property Injection - Method Injection Three ways to pass a dependency instance
  6. !ZPJDIJUHZ Tight Coupling / Loose Coupling Tight Coupling *NQMFNFOUBUJPO $PODSFUF5ZQF

    &YUFSOBM4ZTUFN OFUXPSL pMFTZTUFN  EBUFUJNF SBOEPNOVN
  7. !ZPJDIJUHZ Tight Coupling / Loose Coupling Tight Coupling *NQMFNFOUBUJPO $PODSFUF5ZQF

    &YUFSOBM4ZTUFN OFUXPSL pMFTZTUFN  EBUFUJNF SBOEPNOVN Hard to test
  8. !ZPJDIJUHZ Tight Coupling / Loose Coupling Tight Coupling Loose Coupling

    *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN OFUXPSL pMFTZTUFN  EBUFUJNF SBOEPNOVN *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN *OUFSGBDF 1SPUPDPM Hard to test
  9. !ZPJDIJUHZ Tight Coupling / Loose Coupling Tight Coupling Loose Coupling

    *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN OFUXPSL pMFTZTUFN  EBUFUJNF SBOEPNOVN *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN *OUFSGBDF 1SPUPDPM .PDL Hard to test
  10. !ZPJDIJUHZ Tight Coupling / Loose Coupling Tight Coupling Loose Coupling

    *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN OFUXPSL pMFTZTUFN  EBUFUJNF SBOEPNOVN *NQMFNFOUBUJPO $PODSFUF5ZQF &YUFSOBM4ZTUFN *OUFSGBDF 1SPUPDPM .PDL Hard to test Easy to test
  11. !ZPJDIJUHZ Problem of Dependency Injection Soon... " Dependency # let

    a = A(b: B(c: C())) $ Dependency Dependency Injection Code
  12. !ZPJDIJUHZ let a = A(b: B(c: C(f: F()), e: E(f:

    F()))) Problem of Dependency Injection Much later... $ ' * & # ) " % ( Hard to manage Dependency Injection Code
  13. !ZPJDIJUHZ Dependency Injection Container $ ' * & # )

    " % ( %*$POUBJOFS Dependency Graph Register the dependency graph let a = ... Resolve dependencies upon your request
  14. !ZPJDIJUHZ Dependency Injection Container Pros and Cons • Pros -

    It's easy to use. - Definitions are in a single place.
  15. !ZPJDIJUHZ Dependency Injection Container Pros and Cons • Cons 

    &%*51PTTJCMFSVOUJNFFSSPSDBVTFECZGPSDFVOXSBQQJOH
  16. !ZPJDIJUHZ Cake Pattern: Statically Defined Dependency Injection "CTUSBDU $PNQPOFOU "CTUSBDU

    $PNQPOFOU $PODSFUF $PNQPOFOU $PODSFUF $PNQPOFOU 1SPUPDPM 1SPUPDPM %FpOFTQMBDFIPMEFS GPSEFQFOEFODZ %FpOFTEFQFOEFODZ
  17. !ZPJDIJUHZ Cake Pattern: Statically Defined Dependency Injection "CTUSBDU $PNQPOFOU "CTUSBDU

    $PNQPOFOU $POUFYU $PODSFUF $PNQPOFOU $PODSFUF $PNQPOFOU 1SPUPDPM 1SPUPDPM $MBTT4USVDU&OVN %FpOFTQMBDFIPMEFS GPSEFQFOEFODZ %FpOFTEFQFOEFODZ $PNCJOFTEFQFOEFODZ EFpOJUJPOT
  18. !ZPJDIJUHZ Cake Pattern Pros and Cons • Pros - Lack

    of definitions checked by compiler. • Cons - Less easy than DI container. %FQFOEFODZ*OKFDUJPOXJUIUIF$BLF1BUUFSOJO4XJGUCZ#PC$PUUSFMM
 IUUQBDRVJIJSFNFEFQFOEFODZJOKFDUJPOXJUIUIFDBLFQBUUFSOJOTXJGU (SFBUBSUJDMFUPMFBSO$BLF1BUUFSO
  19. !ZPJDIJUHZ Summary • Basic Concepts - Inversion of Control -

    Tight/loose coupling • Dependency Injection Container - Easy but possible error at runtime • Cake Pattern - Difficult but safe at runtime