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/

Swift India

August 04, 2018
Tweet

More Decks by Swift India

Other Decks in Programming

Transcript

  1. Overview • What is MVC? • Why we hate it?

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

    AC TIO N request state notify
  3. “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
  4. 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
  5. 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 :)
  6. Our expectations from a software architecture Consistency Testability Compile Time

    Performance Development Time Portability Easy Onboarding Reuse Uncertainty
  7. 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
  8. UserSession Controller User View Controller Login ViewController Some Controller App

    Coordinator User APIClient Login APIClient OAuthToken Create non-view controllers to do stuff
  9. ViewController Model firstName: String lastName: String birthdate: Date View ViewData

    displayName: String birthdayString: String childImages: [UIImage] observes observes actions updates Source
  10. 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’’’
  11. 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*
  12. 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*
  13. Decompose across multiple dimensions A B C D E A’

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

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

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

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

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

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

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

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

    D’ E’ A’’ B’’ C’’ E’’ A’’’ B’’’ C’’’ A* A
  22. 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*