MVC: My struggle and path to enlightenment

Flashback

  • invented in 1979
  • by Trygve Reenskaug
  • while with Xerox
  • NOT FOR THE WEB
  • for GUI applications
  • with Smalltalk - 80

By Trygve Reenskaug - Trygve Reenskaug, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=31168223

Smalltalk GUI

MVC for each element on the screen!!!

 A Thing

According to Trygve Reenskaug

Something that is of interest to the user. It could be concrete, like a house or an integrated

circuit. It could be abstract, like a new idea or opinions about a paper. It could be a whole,

like a computer, or a part, like a circuit element

Model

According to Trygve Reenskaug

Models represent knowledge. A model could be a single object (rather uninteresting), or it

could be some structure of objects.

A Model is an active representation of an abstraction in the form of data in a computing

system

View

According to Trygve Reenskaug

A view is attached to its model (or model part) and gets the data necessary for the presentation

from the model by asking questions...

To any given Model there is attached one or more Views, each View being capable of

showing one or more pictorial representations of the Model on the screen and on hardcopy.

... the view will therefore have to know the semantics of the attributes of the model it represents.

Editors

Controllers

According to Trygve Reenskaug

A controller is the link between a user and the system. It provides the user with input by

arranging for relevant views to present themselves in appropriate places on the screen. It

provides means for user output by presenting the user with menus or other means of giving

commands and data.

A controller should never supplement the views...

Conversely, a view should never know about user input, such as mouse operations and

keystrokes.

MVC

MVC

Let's see how this works on the Web

Controllers

According to PHP frameworks

  • Matches a route
  • Instantiates a class that groups actions
  • Calls an action with parameters
  • The action calls stuff on model
  • The action creates a view
  • The action gives model to view
  • The actions renders the view

... so many of them, doing the exact same thing ...

Model

  • The model is your database
  • The model is where all your DB queries reside
  • For each table in your DB you have a model

According to friends and random search results

Model

According to Laravel

  • Defining Models
    • Eloquent Model Conventions
  • Retrieving Models
  • Retrieving Single Models / Aggregates
  • Inserting & Updating Models
  • Deleting Models
  • Query Scopes
  • Events
    • retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored

Model

According to Yii 2.0

  • Attributes
  • Scenarios
  • Validation Rules
  • Massive assignment
  • Data Exporting

What annoys me

  • thin models (they are just DBAL)
  • fat controllers (this is where the actual work is done)
  • this is bad
  • because now we can't reuse our logic

A calculator App

  • No data -> no DB -> no Model ???
  • Have the calculations done in the controller?
  • Use an HTTP controller in a command line app?

Separation of concerns

  • Different controllers for different inputs
  • Different view for different presentations and views
  • Same calculator logic

Implementation

... next time :)

Enlightenment

Enlightenment (to an extent)

Thank you!

  • https://www.facebook.com/mkosturkov
  • @mkosturkov
  • https://www.linkedin.com/in/milko-kosturkov/
  • mkosturkov@gmail.com

Copy of MVC: My struggle and path to enlightenment

By Milko Kosturkov

Copy of MVC: My struggle and path to enlightenment

A presentation about MVC.

  • 643