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

Dependency Injection by Aadithya at SwiftDelhi

Dependency Injection by Aadithya at SwiftDelhi

Dependency Injection by Aadithya at SwiftDelhi

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

July 16, 2017
Tweet

Transcript

  1. Dependency Injection Adithya Reddy iOS engineer at UrbanClap

  2. What. Why. How.

  3. What is dependency Injection? It’s a fancy name for a

    fairly simple concept.
  4. Is DI used in this code snippet below? NO

  5. What about this? YES

  6. And this? NO

  7. Traditional way: Race car Engine Transmission Piston Shaft Clutch Flywheel

  8. Dependency injection way: Race car Engine Transmission Piston Shaft Clutch

    Flywheel
  9. Why use dependency Injection? 3 C’s Clarity Customizability Concerns

  10. Clarity Callers need to know only about one thing, i.e.

    initializer init() is clean and not dumped with constructors No unexpected behavior Example: if a new person joins the team, he need not go throught the viewcontroller to find the dependent objects in it. He just needs to look at init and inject.
  11. Customizability Have control over which implementation to use. For example:

    I can plug in Engine with FastEngine without worrying much Also, highly useful in testing. X1 in debug mode and X2 in release mode.
  12. Concerns Race car is just a race car. Need not

    worry about the configuration of other parts. Specific purposes lying in each component Allows composing instead of subclassing
  13. Constructor injection vs Setter injection Constructor injection : use when

    values dont change Setter injection : use when required. When you want to set the dependencies via properties
  14. How to setup and use dependency injection? Now, I will

    demonstrate how to integrate the Typhoon framework to achieve DI. Concept of Assemblies
  15. Coming back… Now… you must be able to answer why

    the code snippet below doesnt confirm to DI...
  16. Why does this confirm to DI?

  17. And finally, why doesnt this confirm to DI?