Application architecture doesn't have to suck

40
SUCK APPLICATION ARCHITECTURE DOESN’T HAVE TO

Transcript of Application architecture doesn't have to suck

Page 1: Application architecture doesn't have to suck

SUCKAPPLICATION ARCHITECTURE DOESN’T HAVE TO

Page 2: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

WHO AM I?

▸ Jeremy Tregunna

▸ Programming for 29 years

▸ iOS since the unofficial SDK

▸ Twitter: @jtregunna

▸ GitHub: github.com/jeremytregunna

Page 3: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

WHO AM I?

▸ Jeremy Tregunna

▸ Programming for 29 years

▸ iOS since the unofficial SDK

▸ Twitter: @jtregunna

▸ GitHub: github.com/jeremytregunna

▸ Aspiring boat builder

Page 4: Application architecture doesn't have to suck

MVC

Page 5: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

VIEW CONTROLLER RESPONSIBILITIES

▸ Animations

▸ Orientation changes

▸ Laying out views

▸ Handling user input

Page 6: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

VIEW CONTROLLER RESPONSIBILITIES

▸ Animations

▸ Orientation changes

▸ Laying out views

▸ Handling user input

▸ Making network requests

▸ Model Mutation

▸ Synchronizing communication

Page 7: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

VIEW CONTROLLER RESPONSIBILITIES

▸ Animations

▸ Orientation changes

▸ Laying out views

▸ Handling user input

▸ Making network requests

▸ Model Mutation

▸ Synchronizing communication

MASSIVE VIEW

CONTROLLER

Page 8: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

MVC PROBLEMS

▸ Massive view controllers suck

▸ State synchronization

▸ View controller entanglement

▸ Reuse issues abound

▸ Testing

▸ Many more, but that’s another talk.

Page 9: Application architecture doesn't have to suck

MVVM

Page 10: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

VIEW CONTROLLER

▸ Animations

▸ Orientation changes

▸ Binding views to data

VIEW

▸ Handling user input

▸ Laying out subviews

Page 11: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

MVVM PROBLEMS

▸ Data binding support isn’t native

▸ High barrier to entry for new team members

▸ FRP helps, but tends to spread through your code like the plague

▸ Real world examples tend to pair ViewModels and ViewControllers, more like MVC-N

Page 12: Application architecture doesn't have to suck

REDUX

Page 13: Application architecture doesn't have to suck

REDUX-ISH

Page 14: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

Page 15: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

▸ Application state easy to conceptualize

Page 16: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

▸ Application state easy to conceptualize

▸ Unidirectional data flow

Page 17: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

▸ Application state easy to conceptualize

▸ Unidirectional data flow

▸ Changing state means firing an action

Page 18: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

▸ Application state easy to conceptualize

▸ Unidirectional data flow

▸ Changing state means firing an action

▸ State transformers isolated

Page 19: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

INTRODUCTION

▸ Web world

▸ Application state easy to conceptualize

▸ Unidirectional data flow

▸ Changing state means firing an action

▸ State transformers isolated

▸ Foreign, but easy to understand

Page 20: Application architecture doesn't have to suck

CLOSER LOOK

Page 21: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

STOREclass Store<S: State> { var subscriptions: [NewStateDeliverable] = [] var dispatchFunction: ((Action) -> Any)! var state: S! { didSet { subscriptions.forEach { $0.newState(state) } } }

func subscribe(subscriber: NewStateDeliverable) { guard _alreadySubscribed(subscriber) else { return } subscriptions.append(subscribe) if let state = state { subscriber.newState(state) } }

func dispatch(action: Action) -> Any { return dispatchFunction(action) } }

Page 22: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REDUCERstruct CounterReducer: Reducer { func handleAction(action: Action, state: AppState?) -> AppState { var state = state ?? AppState()

switch action { case _ as IncrementAction: state.counter += 1 case _ as DecrementAction: state.counter -= 1 default: break }

return state } }

Page 23: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

ACTIONstruct IncreaseAction: Action {}

struct DecreaseAction: Action {}

// More realistic example…

struct SignInAction: Action { let email: String let password: String }

Page 24: Application architecture doesn't have to suck

NAVIGATION

Page 25: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

PROBLEMS

▸ View controllers tend to know about other view controllers

Page 26: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

PROBLEMS

▸ View controllers tend to know about other view controllers

▸ Complicated state synchronization between VCs to share state changes

Page 27: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

PROBLEMS

▸ View controllers tend to know about other view controllers

▸ Complicated state synchronization between VCs to share state changes

▸ External routers contain some of these problems, VCs still dependent

Page 28: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

PROBLEMS

▸ View controllers tend to know about other view controllers

▸ Complicated state synchronization between VCs to share state changes

▸ External routers contain some of these problems, VCs still dependent

▸ Lack of dynamic hierarchies

Page 29: Application architecture doesn't have to suck

SOLUTIONS?

Page 30: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCKTEXT

FIRE AN ACTION

▸ Track navigation state as part of our application state, because it is.

▸ Router acts as a reducer over navigation state

▸ Fire an action when you want to present something

Page 31: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCKTEXT

RESPONDER CHAIN

▸ Built in

▸ Passes messages along a chain of responsibility

▸ Message ignored if nobody can answer it

▸ Familiar pattern

Page 32: Application architecture doesn't have to suck

DEMO

Page 33: Application architecture doesn't have to suck

WHY?

Page 34: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REASONS FOR THIS ARCHITECTURE

▸ MVC defers complexity to controller level

Page 35: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REASONS FOR THIS ARCHITECTURE

▸ MVC defers complexity to controller level

▸ MVVM non-native binding mechanism

Page 36: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REASONS FOR THIS ARCHITECTURE

▸ MVC defers complexity to controller level

▸ MVVM non-native binding mechanism

▸ Actions can be persisted, and replayed

Page 37: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REASONS FOR THIS ARCHITECTURE

▸ MVC defers complexity to controller level

▸ MVVM non-native binding mechanism

▸ Actions can be persisted, and replayed

▸ Avoids hidden dependencies

Page 38: Application architecture doesn't have to suck

APPLICATION ARCHITECTURE DOESN’T HAVE TO SUCK

REASONS FOR THIS ARCHITECTURE

▸ MVC defers complexity to controller level

▸ MVVM non-native binding mechanism

▸ Actions can be persisted, and replayed

▸ Avoids hidden dependencies

▸ State manipulation is standard

Page 39: Application architecture doesn't have to suck

QUESTIONS?

Page 40: Application architecture doesn't have to suck

THANK YOU!https://github.com/Greenshire/Calibre