51
(B)VIPER MODULES BREAK THE MONOLITH WITH Nicola Zaghini - [email protected] - @nzaghini

Break the monolith with (B)VIPER Modules

Embed Size (px)

Citation preview

Page 1: Break the monolith with (B)VIPER Modules

(B)VIPER MODULES

BREAK THE MONOLITH WITH

Nicola Zaghini - [email protected] - @nzaghini

Page 2: Break the monolith with (B)VIPER Modules

UNCLE BOB HTTPS://8THLIGHT.COM

Page 3: Break the monolith with (B)VIPER Modules

MUTUAL MOBILE ENGINEERING

Page 4: Break the monolith with (B)VIPER Modules

FANCY A SLICE 🍰😬 ?

Page 5: Break the monolith with (B)VIPER Modules

We strive to make travel better and to enable our customers to shape new and

superior travel experiences with innovative, personalised mobile and

digital offerings.

Page 6: Break the monolith with (B)VIPER Modules

The road to (B)VIPER

DEFINING THE MODULE

Page 7: Break the monolith with (B)VIPER Modules

FlightSearch

FlightAvailability

FlightSummary

WHAT IS AN APPLICATION ?

Page 8: Break the monolith with (B)VIPER Modules

FlightSearch

FlightAvailability

FlightSummary

WHAT IS A MODULE ?

Page 9: Break the monolith with (B)VIPER Modules

HOW DO WE WANT MODULES?Composable

Application = SUM ( Modules )Module = SUM ( Components )

Black boxExpose clear APIs.

The outside world should not know internal details.

Testable“Isolation checks” made simple!

Independent of User Interface User interface must change without impacting the rest of the system.

Independent of FrameworksDatabase, UI framework, Network, …

SimpleSmall and well defined parts delivering one function.

Page 10: Break the monolith with (B)VIPER Modules

MODULES

Page 11: Break the monolith with (B)VIPER Modules

APPLICATION = SUM (MODULES )

Page 12: Break the monolith with (B)VIPER Modules

Availability ServiceBooking ServicePayment ServiceCheck-in Service

Mobile Application

Remote Services

DESIGN THE MODULE

C4 MODELS A WAY OF COMMUNICATING SOFTWARE SYSTEMS

Page 13: Break the monolith with (B)VIPER Modules

Mobile Application

Flight Search

DESIGN THE MODULE

Availability ServiceBooking ServicePayment ServiceCheck-in Service

Flight Availability

Flight Summary

Services &

Entities

Page 14: Break the monolith with (B)VIPER Modules

Services &

Entities

FlightAvailability

Flight Search

Flight Summary

Page 15: Break the monolith with (B)VIPER Modules

FlightAvailability

UIView Controller

Flight Search

Flight Summary

Services &

Entities

Page 16: Break the monolith with (B)VIPER Modules

1K+ lines UIViewController 😱?

Page 17: Break the monolith with (B)VIPER Modules

I think we can do better

😎

Page 18: Break the monolith with (B)VIPER Modules

View ModelView

User Interface No B. Logic

Data Model Converter for the View Apply Application BL

Routing

Flight Search

Flight Summary

MVVM APPROACH

Services &

Entities

Page 19: Break the monolith with (B)VIPER Modules

Domain Model to View Model mapping

User interaction

Apply Business Rules

Routing

Overall, there are too many reason to change the ViewModel - Single responsibility principle violated

VIEW MODEL RESPONSIBILITIES

Page 20: Break the monolith with (B)VIPER Modules

View Model

FlightAvailability

View

User Interface No B. Logic

Flight Search

Flight Summary

Data Model Converter for the View Listen to View events Apply Application BL

Routing

Services &

Entities

Page 21: Break the monolith with (B)VIPER Modules

View Model

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

RouterFlight Search

Flight Summary

Data Model Converter for the View Listen to View events Apply Application BL

Services &

Entities

Page 22: Break the monolith with (B)VIPER Modules

protocol WeatherListRouter { func navigateToDetail(location: Location) func navigateToAddWeatherLocation() }

ROUTER CONTRACT

Page 23: Break the monolith with (B)VIPER Modules

View Model

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

RouterFlight Search

Flight Summary

Data Model Converter for the View Listen to View events Apply Application BL

Services &

Entities

Page 24: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listen to View events

Apply App BL

Data Model Converter

for the View

Flight Search

Flight Summary

Services &

Entities

Page 25: Break the monolith with (B)VIPER Modules

protocol WeatherListPresenter { func loadContent() func presentWeatherDetail(location: String) func presentAddWeatherLocation() }

PRESENTER CONTRACT

Page 26: Break the monolith with (B)VIPER Modules

protocol WeatherListView { func displayLocationList(vm: LocationListVM) func displayError(errorMessage: String) }

VIEW CONTRACT

Page 27: Break the monolith with (B)VIPER Modules

struct LocationListViewModel { let locations: [LocationViewModel] }

struct LocationViewModel { let locationId: String let name: String let detail: String }

VIEW MODEL

Page 28: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Data Model Converter

for the View

Flight Search

Flight Summary

Listen to View events

Apply App BR

Services &

Entities

Page 29: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Apply Application Business Rules

Flight Search

Flight Summary

Services &

Entities

Page 30: Break the monolith with (B)VIPER Modules

public protocol WeatherListInteractor { func locations() -> [Location] }

INTERACTOR CONTRACT

Page 31: Break the monolith with (B)VIPER Modules

PresenterView

Next Module Navigation

Router

View Model

Interactor

- loadContent- presentWeatherDetail

Flight Search

- locations

LocationListViewModel

- displayLocations

- navigateToWeatherDetail

COMPONENTS INTERACTION

Page 32: Break the monolith with (B)VIPER Modules

Cool, are we there yet?

…nope 🚀

Page 33: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Flight Search

Flight Summary?🤔?

Apply Application Business Rules

Services &

Entities

Page 34: Break the monolith with (B)VIPER Modules

Presenter😐

User Interface No B. Logic

Next Module Navigation

😐

😐

Listens to View events

Data Model Converter

for the View

😐

Flight Search

Flight Summary?🤔?

Apply Application Business Rules

TESTING IS SIMPLE

Services &

Entities

Page 35: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Flight Search

Flight Summary?🤔?

Apply Application Business Rules

Services &

Entities

Page 36: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Flight Search

Flight Summary

Builder

😀

Creates and wires

components

Apply Application Business Rules

Services &

Entities

Page 37: Break the monolith with (B)VIPER Modules

Knows how to wire the Module components.Knows concrete Components implementation.

Dependency Injection.

Module exposed as Black Box ViewController to the outside world.

InputParameters

ViewController

THE BUILDER COMPONENT

Builder

Page 38: Break the monolith with (B)VIPER Modules

public protocol WeatherListBuilder { func buildModule() -> UIViewController? }

BUILDER CONTRACT

Page 39: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Flight Search

Flight SummaryBuilder

Creates and wires

components

Apply Application Business Rules

Services &

Entities

Page 40: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

My View

User Interface No B. Logic

Next Module Navigation

My Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Flight Search

Flight SummaryBuilder

Creates and wires

components

Apply Application Business Rules

Services &

Entities

Page 41: Break the monolith with (B)VIPER Modules

Presenter

FlightAvailability

View

User Interface No B. Logic

Next Module Navigation

Router

View Model

Listens to View events

Data Model Converter

for the View

Interactor

Apply Application Business Rules

Services &

Entities

Page 42: Break the monolith with (B)VIPER Modules

FANCY A SLICE 🍰😬 ?

Entities and Services

Interactor

Presenter

View Model

View

Router

MODULE

(B)uilder

Page 43: Break the monolith with (B)VIPER Modules

Current status.

Page 44: Break the monolith with (B)VIPER Modules

Composable ✅ Application = SUM ( Modules )Module = SUM ( Components )

Black box ✅The outside world should not know internal details.

Exposes clear APIs.

Testable ✅Promoting easy isolation checks of the parts and the whole.

Independent of User Interface ✅User interface must change easily without impacting the rest of the system.

Independent of Frameworks ✅Database, UI framework, Network, …

Simple ✅Small and well defined parts delivering one function.

Page 45: Break the monolith with (B)VIPER Modules

Sending information between (B)VIPER Modules

… what if To Module is modal?To_Module accepts a delegate which is a component of the From_Module (usually the Presenter)

Router

From Module To Module

Router provides out data needed

by the next Builder

Builder

TIP #1

Page 46: Break the monolith with (B)VIPER Modules

Window

Router

InteractorView (VC) Presenter

References among components

Strong

Weak

Strong

Weak

Weak

Strong

Strong

When the ViewController is deallocated, the whole module gets deallocated.

TIP #2

Page 47: Break the monolith with (B)VIPER Modules

Crossing boundaries Models

TIP #3

Page 48: Break the monolith with (B)VIPER Modules

Dependency Management Toolsupporting Semantic Versioning!

(CocoaPod, Carthage)

Dependency Injection Container

(Typhoon, Swinject)

TIP #4

Page 49: Break the monolith with (B)VIPER Modules

INTEGRATED TEST ARE A SCAM by J.B. Rainsberger

SIMPLE MADE EASY by Rich Hickey

WATCH LIST SUGGESTIONS 👀

CLEAN ARCHITECTURE AND DESIGN by Robert C. Martin

Page 50: Break the monolith with (B)VIPER Modules

No such thing asone-size fits all.

Page 51: Break the monolith with (B)VIPER Modules

@nzaghini [email protected]

(B)VIPER example repo: http://bit.ly/B_VIPER

I use unsplash.com to make slides look great!