Clean architecture workshop

50
#Swift3Arch Implementing Clean Architecture in iOS Jorge D. Ortiz-Fuentes @jdortiz

Transcript of Clean architecture workshop

#Swift3Arch

Implementing Clean Architecture in iOS

Jorge D. Ortiz-Fuentes @jdortiz

A Canonical Examples production

#Swift3Arch

#Swift3Arch

Agenda★Goals

★Before Clean Architecture

★Clean Architecture Concepts

★ Implementation rules

★Example App: RealProgrammers

★ Implement 1st User Story

★References

#Swift3Arch

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

#Swift3Arch

LIVE Online Training★3 days (same weekday 3 continuous weeks)

★Máx 10 people

★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!

More info at: http://canonicalexamples.com

Goals

#Swift3Arch

Goals

★Understand the concepts

★ Learn the sources

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

★Grow from here

Looking for volunteer

Before Clean Architecture

Warning: Background Ahead!

#Swift3Arch

Before Clean Architecture

★MVC

★MVP

★MVVM

– 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.”

#Swift3Arch

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

Presenter / View Model] = [TheApp]

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

★Responsibilities matter!

★Other components (patterns) might be involved.

#Swift3Arch

The goal★Separation of responsibilities into roles. Is

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

★Now why?

• Testability

• Reusability

• Extensibility

#Swift3Arch

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)

#Swift3Arch

Apple’s MVC★View

• Only UIView subclasses

• Show information and handle input to generate higher level events passed to controller

• Fully reusable views library. Consistent L&F

★Controller

• Views delegate / control what’s displayed

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

• Knows about changes in the model and update what is displayed on the view. Typically a UIViewController. Responsible of presentation logic.

★Model

• Contain the business logic. No references to the UI.

Flow synchronization vs Observer synchronization

MVC = Composite + Strategy + Observer

Apple’s MVC

Ctrlr

ModelView

#Swift3Arch

Apple’s 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.

MVP

MVP

Presenter ModelView

Events Operations

Changes Changes

MVVM

MVVM

View Model ModelView

Events & Info Operations

Changes Changes

Clean Architecture Concepts

#Swift3Arch

Clean Architecture

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

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

Clean ArchitectureApp

Delegate

View (VC) Presenter Interactor Entity Gateway

Connector

Persistance FW

View

Netw

ork

Location FW

Presenter

Entity Gateway

Clean Architecture

Interactor

Entity

Implementation Rules

#Swift3Arch

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??

Example App: RealProgrammers

#Swift3Arch

Features★Add potential candidates (Name, email, and important skills)

★Show list of programmers 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

#Swift3Arch

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

★Show list of programmers 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

Implementing the 1st User Story

I feel like I could Take On the

World!

#Swift3Arch

First User Story

★Show a list of data elements to the user.

Interactor

View (VC) Presenter Show Programmers

Entity Gateway

#Swift3Arch

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

Presenter

View (VC) ProgrammersList

Show Programmers

Entity Gateway

#Swift3Arch

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!)

View

Programmers TableVC

ProgrammersList

Show Programmers

Entity Gateway

#Swift3Arch

View

★Combination of Views + View Controller

★Make it dumb (passive), but useful

★Tell the presenter about the relevant events

★Keep them decoupled

Presenter

Programmers TableVC

ProgrammersList

Show Programmers

Entity Gateway

#Swift3Arch

Back to the Presenter

★ Implement responses to the user events.

★Use the interactor

★Add presentation logic to the presenter part

#Swift3Arch

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 lazy instantiation for the current date

Entity Gateway

Programmers TableVC

ProgrammersList

Show Programmers

Entity Gateway

#Swift3Arch

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.

References

#Swift3Arch

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

Thank you!

@jdortiz #Swift3Arch