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

Improving Model View Controller Architecture

Improving Model View Controller Architecture

We normally tend to use MVC in our early days as an iOS engineer and as time passes - shift on to other architectures because of the challenges that arise on a code level, causing maintenance, feature development challenges in the long run. Still interested to use MVC and not frown upon whenever you open an MVC based project after a long time? Look nowhere but here!

Shivam has been working as an iOS engineer for about 2 years. Just getting started in his career, Shivam is always on the lookout for anything new in iOS/tech in general.
https://www.linkedin.com/in/shivam-kapur-8aab05106

Presented at Swift Mumbai - Chapter #5 Meetup: https://www.meetup.com/Swift-Mumbai/events/252627724/

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

August 04, 2018
Tweet

Transcript

  1. MVC in 2018? 
 Sure, why not. Shivam Kapur 


    iOS Engineer @ heady.io
  2. Overview • What is MVC? • Why we hate it?

    • Our expectations from a software architecture • How can we improve?
  3. MVC CONTROLLER MODEL VIEW UPDATE U PD ATE U SER

    AC TIO N request state notify
  4. Apple’s Version of MVC CONTROLLER MODEL VIEW UPDATE U PD

    ATE U SER AC TIO N notify
  5. Why we hate it?

  6. “One can merge the MVC roles played by an object,

    making an object, for example, fulfill both the controller and view roles—in which case, it would be called a view controller.” Model-View-Controller — developer.apple.com
  7. MODEL View+Controller MASSIVE VIEW CONTROLLER

  8. But when you get overly attached to MVC, then you

    look at every class you create and ask the question “is this a model, a view, or a controller?”. Because this question makes no sense, the answer doesn’t either: anything that isn’t evidently data or evidently graphics gets put into the amorphous “controller” collection, which eventually sucks your entire codebase into its innards like a black hole collapsing under its own weight Graham Lee, Inside-Out Apps
  9. Do all objects have to be model, view or controller?

    • There has to be atleast that split! • Not all objects are model, view or controller. • Eg. GestureRecognizer, Parsing JSON • Different layers in the application such as Networking, LocationAccessHandler, Database etc. • MVC does not structure your whole application :)
  10. Our expectations from a software architecture Consistency Testability Compile Time

    Performance Development Time Portability Easy Onboarding Reuse Uncertainty
  11. Understandability • Locally • Feature Level

  12. Replacability • Reuse? • It is expensive. • Only 20%

    of written software intended for reuse is actually reused -_- • Designing for reuse leads to bad API • Replace Frameworks
  13. How can we improve?

  14. Composition

  15. Composition?

  16. Decomposition is the key!

  17. ViewController TableView Model DataSource Delegate Data Fetching IndexPaths User Actions

    Setup Source
  18. DataSource ViewController TableView Model DataSource Delegate Data Fetching IndexPaths User

    Actions Setup Source
  19. DataSource ViewController TableView Model DataSource Delegate Data Fetching IndexPaths User

    Actions Setup Source
  20. Delegate DataSource ViewController TableView Model DataSource Delegate Data Fetching IndexPaths

    User Actions Setup coordinates Source
  21. MainDataSource StoriesData Source FeedDataS ource DataSource 3 DataSource 4 Source

  22. UserSession Controller User View Controller Login ViewController Some Controller App

    Coordinator User APIClient Login APIClient OAuthToken Create non-view controllers to do stuff
  23. ViewController Model View name: String birthdate: Date numberOfChildren: Int Source

  24. ViewController Model firstName: String lastName: String birthdate: Date View ViewData

    displayName: String birthdayString: String childImages: [UIImage] observes observes actions updates Source
  25. View Controller Model Networking Many Decomposition Dimensions Technical

  26. Many Decomposition Dimensions Visual View Controller Model Networking Technical

  27. Many Decomposition Dimensions Domain Visual View Controller Model Networking Technical

  28. Many Decomposition Dimensions Domain 1. 2. 3. Visual View Controller

    Model Networking Technical
  29. Many Decomposition Dimensions Domain 1. 2. 3. Temporal Visual View

    Controller Model Networking Technical
  30. We often focus only on the technical dimension View Controller

    Model Networking A B C D E A’ B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’ E’’’
  31. We often focus only on the technical dimension View Controller

    Model Networking A B C D E A’ B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’ E’’’ ViewModel A* B* C* D* E*
  32. We often focus only on the technical dimension View Router

    Model Networking A B C D E A’ B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’ E’’’ Interactor Aº Bº Cº Dº Eº Presenter A* B* C* D* E*
  33. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’ E’’’
  34. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’
  35. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’
  36. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ D’’ E’’ A’’’ B’’’ C’’’ D’’’
  37. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’
  38. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’
  39. Decompose across multiple dimensions A B C D E A’

    B’ C’ D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’
  40. Decompose across multiple dimensions A C D E A’ B’

    C’ D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’
  41. Decompose across multiple dimensions C D E A’ B’ C’

    D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’ A* A
  42. Technical Dimension Only A C D E A’ B’ C’

    D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’ Aº Bº Cº Eº A* B* C* D* E*
  43. That’s all folks :)