Slide 1

Slide 1 text

Dependency To inject or not?

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

What is the problem in your code base?

Slide 4

Slide 4 text

“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.

Slide 5

Slide 5 text

“Unmaintainable Codebase”

Slide 6

Slide 6 text

“Non testable code”

Slide 7

Slide 7 text

“Difficulty for fellow peers”

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Nobody wants that right? $

Slide 10

Slide 10 text

Enter Dependency Injection!

Slide 11

Slide 11 text

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.

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

3 Current •Initialiser Based •Property Based •Parameter Based

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

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.

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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.

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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.

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Weaver to the Rescue!!

Slide 33

Slide 33 text

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.

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

Registering Dependencies

Slide 38

Slide 38 text

Defining Scope

Slide 39

Slide 39 text

Building Root View Controller

Slide 40

Slide 40 text

MovieManager

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Fin.