Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Nicola Zaghini: BREAK THE MONOLITH WITH (B)VIPER MODULES. OR HOW DO I START MODULARISING MY CODE FOR A BETTER (ENGINEERING) LIFE.

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
October 22, 2016

Nicola Zaghini: BREAK THE MONOLITH WITH (B)VIPER MODULES. OR HOW DO I START MODULARISING MY CODE FOR A BETTER (ENGINEERING) LIFE.

In the software engineering business there is no one fits all solution but when design patterns arise in the community it is important to understand the reasons why they were originated and how to tweak them for our purposes. This talk will explore the VIPER design with top-down approach, deriving the design decisions from quality attributes of a software system including modularity, reusability, testability, single responsibility and more. You will learn the reasons why the design was initially conceived (Clean Architecture by Uncle Bob) then formalised for mobile (by Mutual Mobile) and how to revise it to fit your purposes. Eventually a modified version of the design, friendly called (B)VIPER, will be discussed as improvement for better reuse of modules among different teams.

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

October 22, 2016
Tweet

Transcript

  1. (B)VIPER MODULES BREAK THE MONOLITH WITH Nicola Zaghini - nzaghini@gmail.com

    - @nzaghini
  2. UNCLE BOB HTTPS://8THLIGHT.COM

  3. MUTUAL MOBILE ENGINEERING

  4. FANCY A SLICE ?

  5. 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.
  6. The road to 
 (B)VIPER DEFINING 
 THE MODULE

  7. Flight
 Search Flight
 Availability Flight
 Summary WHAT IS AN APPLICATION

    ?
  8. Flight
 Search Flight
 Availability Flight
 Summary WHAT IS A MODULE

    ?
  9. HOW DO WE WANT MODULES? Composable
 Application = SUM (

    Modules )
 Module = SUM ( Components ) Black box
 Expose 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 Frameworks
 Database, UI framework, Network, … Simple
 Small and well defined parts delivering one function.
  10. MODULES

  11. APPLICATION = SUM (MODULES )

  12. Availability Service
 Booking Service
 Payment Service
 Check-in Service
 … Mobile

    Application Remote Services DESIGN THE MODULE C4 MODELS A WAY OF COMMUNICATING SOFTWARE SYSTEMS
  13. Mobile Application Flight Search DESIGN THE MODULE Availability Service
 Booking

    Service
 Payment Service
 Check-in Service
 … Flight Availability Flight Summary Services & Entities
  14. Services & Entities Flight
 Availability Flight Search Flight Summary

  15. Flight
 Availability UIView Controller Flight Search Flight Summary Services &

    Entities
  16. 1K+ lines 
 UIViewController ?

  17. I think we can do better

  18. View Model View User Interface
 No B. Logic Data Model

    Converter for the View Apply Application BL Routing Flight Search Flight Summary MVVM APPROACH Services & Entities
  19. 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
  20. View Model Flight
 Availability 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
  21. View Model Flight
 Availability View User Interface
 No B. Logic

    Next Module Navigation Router Flight Search Flight Summary Data Model Converter for the View Listen to View events Apply Application BL Services & Entities
  22. protocol WeatherListRouter { func navigateToDetail(location: Location) func navigateToAddWeatherLocation() } ROUTER

    CONTRACT
  23. View Model Flight
 Availability View User Interface
 No B. Logic

    Next Module Navigation Router Flight Search Flight Summary Data Model Converter for the View Listen to View events Apply Application BL Services & Entities
  24. Presenter Flight
 Availability 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
  25. protocol WeatherListPresenter { func loadContent() func presentWeatherDetail(location: String) func presentAddWeatherLocation()

    } PRESENTER CONTRACT
  26. protocol WeatherListView { func displayLocationList(vm: LocationListVM) func displayError(errorMessage: String) }

    VIEW CONTRACT
  27. struct LocationListViewModel { let locations: [LocationViewModel] } struct LocationViewModel {

    let locationId: String let name: String let detail: String } VIEW MODEL
  28. Presenter Flight
 Availability 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
  29. Presenter Flight
 Availability 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
  30. public protocol WeatherListInteractor { func locations() -> [Location] } INTERACTOR

    CONTRACT
  31. Presenter View Next Module Navigation Router View Model Interactor -

    loadContent
 - presentWeatherDetail Flight Search - locations LocationListViewModel - displayLocations - navigateToWeatherDetail COMPONENTS INTERACTION
  32. Cool, are we there yet? …nope

  33. Presenter Flight
 Availability 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
  34. 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
  35. Presenter Flight
 Availability 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
  36. Presenter Flight
 Availability 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
  37. Knows how to wire the Module components.
 Knows concrete Components

    implementation.
 Dependency Injection.
 Module exposed as Black Box 
 ViewController to the outside world. Input
 Parameters View
 Controller THE BUILDER COMPONENT Builder
  38. public protocol WeatherListBuilder { func buildModule() -> UIViewController? } BUILDER

    CONTRACT
  39. Presenter Flight
 Availability 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
  40. Presenter Flight
 Availability 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 Summary Builder Creates and wires components Apply Application
 Business Rules Services & Entities
  41. Presenter Flight
 Availability 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
  42. FANCY A SLICE ? Entities and Services Interactor Presenter View

    Model View Router MODULE (B)uilder
  43. Current status.

  44. 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.
  45. 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
  46. Window Router Interactor View (VC) Presenter References among components Strong

    Weak Strong Weak W eak Strong Strong When the ViewController is deallocated, the whole module gets deallocated. TIP #2
  47. Crossing boundaries Models TIP #3

  48. Dependency Management Tool
 supporting Semantic Versioning! (CocoaPod, Carthage) Dependency Injection

    Container (Typhoon, Swinject) TIP #4
  49. 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
  50. No such thing as
 one-size fits all.

  51. @nzaghini
 nzaghini@gmail.com (B)VIPER example repo: 
 http://bit.ly/B_VIPER I use unsplash.com

    to make slides look great!