iOS advanced architecture workshop 3h edition

52
#Swift3Arch Implementing Clean Architecture for iOS Jorge D. Ortiz-Fuentes @jdortiz

Transcript of iOS advanced architecture workshop 3h edition

Page 1: iOS advanced architecture workshop 3h edition

#Swift3Arch

Implementing Clean Architecture

for iOS Jorge D. Ortiz-Fuentes

@jdortiz

Page 2: iOS advanced architecture workshop 3h edition

A Canonical Examples production

#Swift3Arch

Page 3: iOS advanced architecture workshop 3h edition

#Swift3CA

Agenda★Goals

★Before Clean Architecture

★Clean Architecture Concepts

★ Implementation rules

★Example App: RealProgrammers

★ Implement 1st User Story

★Connect the pieces

★References

Page 4: iOS advanced architecture workshop 3h edition

#Swift3CA

Full Version: 3d★MVX

• Learn

• Extract MVP

★1st User Story

• Use Case

• Presenter

• View

• Entity Gateway

• Initial connection

★Connectors

• Memory Management

• Reuse

★2nd user story: add

• Navigation: Modal vs Push

• Command pattern

• Immutables

• Observation

★3rd user story: detail

• Identity

• Use case factories

• 2nd use case in the same view

★Other use cases: just logic

★ Forward flow synchronization

★Dependency Injection

★Asynchrony

• Async entity gateway

• Other tasks

Page 5: iOS advanced architecture workshop 3h edition

#Swift3CA

LIVE Online Training

★3 days (same weekday 3 continuous weeks)

★10 people max

★Live coding shared via video conference

★Chat running all the time

★Complete repo with many commits and Tests> 95%

★Additional exercises

★Ask as much as you want!

Page 6: iOS advanced architecture workshop 3h edition

More info at: http://canonicalexamples.com

Page 7: iOS advanced architecture workshop 3h edition

Goals

Page 8: iOS advanced architecture workshop 3h edition

#Swift3CA

Goals

★Understand the concepts

★ Learn the sources

★ Implement the ideas of the Clean Architecture in a real app

★Grow from here

Page 9: iOS advanced architecture workshop 3h edition

Looking for volunteers

Page 10: iOS advanced architecture workshop 3h edition

Warning: Background Ahead!

Page 11: iOS advanced architecture workshop 3h edition

Before Clean Architecture

Page 12: iOS advanced architecture workshop 3h edition

#Swift3CA

Before Clean Architecture

★MVC

★MVP

★MVVM

Page 13: iOS advanced architecture workshop 3h edition

– Martin Fowler

“Different people reading about MVC in different places take different ideas from it and describe these as 'MVC'. If this doesn't

cause enough confusion you then get the effect of misunderstandings of MVC that develop through a system of

Chinese whispers.”

Page 14: iOS advanced architecture workshop 3h edition

#Swift3CA

Tricky question★ If [Model] + [View] + [Controller /

Presenter / View Model] = [TheApp]

★How can Controller != Presenter != View Model?

★Responsibilities matter!

★Other components (patterns) might be involved.

Page 15: iOS advanced architecture workshop 3h edition

#Swift3CA

The goal★Separation of responsibilities into roles. Is

it? Not historically. Other motivations. Solving problem d’jour.

★Now why?

• Testability

• Reusability

• Extensibility

Page 16: iOS advanced architecture workshop 3h edition

#Swift3CA

MVC: The early days

★Separated presentation: Model <-> Presentation (= V+C)

★Observer originated from MVC, but observing full object (properties = scalars)

★Variations: Passive model, i.e. model cannot send updates (for example HTTP)

Page 17: iOS advanced architecture workshop 3h edition

#Swift3CA

Apple’s MVC

Ctrlr

ModelView

Page 18: iOS advanced architecture workshop 3h edition

#Swift3CA

MVC: Composite Pattern

★Composite

★Strategy

★Observer

Page 19: iOS advanced architecture workshop 3h edition

#Swift3CA

iOS MVC★View

• Only UIView subclasses

• Show information and handle events to controller

• Fully reusable library of views. Consistent L&F

★Controller

• Views delegate+dataSources control what’s displayed

• Receive the events and converts them into calls to the model

• Observe the model and update what is displayed on the view

• Implement presentation logic

★Model

• Implements domain knowledge / business logic. Provides data and commands

• Can be observed

• No references to the UI

Flow synchronization vs Observer synchronization

Page 20: iOS advanced architecture workshop 3h edition

#Swift3CA

MVC: testability★The views are somebody else's (Apple’s) problem.

★Model is easy to test.

★Controller is huge & has dependencies on the model and on the views => Complex to test.

★Too many levels of abstraction, only a few methods exposed.

★Some stuff in view controller that it is usually not tested.

Page 21: iOS advanced architecture workshop 3h edition

MVP

Page 22: iOS advanced architecture workshop 3h edition

#Swift3CA

MVP

Presenter ModelView

Events Operations

Changes Changes

Page 23: iOS advanced architecture workshop 3h edition

MVVM

Page 24: iOS advanced architecture workshop 3h edition

#Swift3CA

MVVM

View Model ModelView

Events & Info Operations

Changes Changes

Page 25: iOS advanced architecture workshop 3h edition

Clean Architecture Concepts

Page 26: iOS advanced architecture workshop 3h edition

What is missing?

Page 27: iOS advanced architecture workshop 3h edition

#Swift3CA

Clean Architecture

★Uncle Bob’s (Robert C. Martin’s) architecture

★Based on Onion, in turn based in hexagonal, DCI and some others

Page 28: iOS advanced architecture workshop 3h edition

#Swift3CA

Clean Architecture: iOS

App Delegate

View (VC) Presenter Interactor Entity Gateway

Connector

Page 29: iOS advanced architecture workshop 3h edition

#Swift3CA

Persistance FW

View

Netw

ork

Location FWPresenter

Entity Gateway

Clean Architecture

Interactor

Entity

Page 30: iOS advanced architecture workshop 3h edition

Implementation Rules

Page 31: iOS advanced architecture workshop 3h edition

#Swift3CA

Rules★We are going to develop one module at a

time

★Follow the dependency rules

★YAGNI

★No early optimization applied

★ It is easy to add tests, but no TDD in 3h OK??

Page 32: iOS advanced architecture workshop 3h edition

Example App: RealProgrammers

Page 33: iOS advanced architecture workshop 3h edition

#Swift3CA

Features★Add potential candidates (Name and important skills)

★Show list of candidates with relative interview date

★Remove candidates from list

★Edit the data of any candidate

★Contact candidate by email

★Sync between devices

★Universal app

★Eye candy

★ iOS/macOS/watchOS/tvOS?

★Credits

Page 34: iOS advanced architecture workshop 3h edition

#Swift3CA

MVP Features★Add potential candidates (Name and important skills)

★Show list of candidates with relative interview date

★Remove candidates from list

★Edit the data of any candidate

★Contact candidate by email

★Sync between devices

★Universal app

★Eye candy

★ iOS/macOS/watchOS/tvOS?

★Credits

Page 35: iOS advanced architecture workshop 3h edition

Implementing the 1st User Story

Page 36: iOS advanced architecture workshop 3h edition

I feel like I could Take On the World!

Page 37: iOS advanced architecture workshop 3h edition

#Swift3CA

First User Story

★Show a list of data elements to the user.

Page 38: iOS advanced architecture workshop 3h edition

#Swift3CA

Interactor

View (VC) Presenter Show Programm

Entity Gateway

Page 39: iOS advanced architecture workshop 3h edition

#Swift3CA

Interactor

★Grab data from the entity gateway

★Convert it to what is needed to be presented

★Pass the results to the presenter

★Start by defining the protocols

Page 40: iOS advanced architecture workshop 3h edition

#Swift3CA

Presenter

View (VC) Programmers

Show Programm

Entity Gateway

Page 41: iOS advanced architecture workshop 3h edition

#Swift3CA

Presenter

★Direction: only from interactor to view

★Configure the (dumb) view with the data provided to it

★Create the protocol for the view (Simplest wins!)

Page 42: iOS advanced architecture workshop 3h edition

#Swift3CA

View

Programmers

Programmers

Show Programm

Entity Gateway

Page 43: iOS advanced architecture workshop 3h edition

#Swift3CA

View

★Combination of Views + View Controller

★Make it dumb (passive), but useful

★Tell the presenter about the relevant events

★Keep them decoupled

Page 44: iOS advanced architecture workshop 3h edition

#Swift3CA

Presenter

Programmers

Programmers

Show Programm

Entity Gateway

Page 45: iOS advanced architecture workshop 3h edition

#Swift3CA

Back to the Presenter

★ Implement responses to the user events.

★Use the interactor

★Add presentation logic to the presenter part

Page 46: iOS advanced architecture workshop 3h edition

#Swift3CA

Presentation Logic

★Date should be relative (“Today”, “1d ago”, “2w ago”)

★That means current date is a dependency that we want to control for tests

★Use default value for the current date

Page 47: iOS advanced architecture workshop 3h edition

#Swift3CA

Entity Gateway

Programmers

Programmers

Show Programm

Entity Gateway

Page 48: iOS advanced architecture workshop 3h edition

#Swift3CA

Entity Gateway

★Basic

★Defer the decision of the persistence framework for later.

★ Implement the minimum functionality in a basic object.

★ Implications of the repository pattern.

Page 49: iOS advanced architecture workshop 3h edition

References

Page 50: iOS advanced architecture workshop 3h edition

#Swift3CA

References

★http://martinfowler.com/eaaDev/uiArchs.html

★https://blog.8thlight.com/uncle-bob/2012/08/13/the-clean-architecture.html

★https://cleancoders.com/episode/clean-code-episode-7/show

Page 51: iOS advanced architecture workshop 3h edition

Thank you!

Page 52: iOS advanced architecture workshop 3h edition

@jdortiz #Swift3Arch