MVC: My struggle and path to enlightenment

Disclaimer

A personal story. Not going too deep on technicalities.

 

A story

  • Discovering the need of a wheel
  • Inventing the wheel
  • Realization that the wheel was already invented
  • Trying out squares, triangles and other shapes for the wheel
  • Having a nice round object
  • Realization of how hard it is to maintain my code
  • Doing a super crappy version of MVC without knowing it
  • Reading about MVC on Wikipedia
  • Trying out implementations, frameworks, talking to colleagues
  • Code that I wrote and actually liked

First job

  • SMS services
  • A couple of rather complex sites (for their time)
  • A video sharing portal (first MVC attempt)

All PHP code

(That I had seen)

<?php
include "path/to/db_con.php";
$res = mysql_query("SELECT * FROM users ORDER BY " . $_GET['order'] . " LIMIT 100");

echo '<table>';
while ($user = mysql_fetch_assoc($res)) {
    echo '<tr>';
    echo "<td>$user[name]</td><td>$user[age]</td><td>";
    if ($user['sex'] == 1) {
        echo "Male";
    } else {
        echo "Female";
    }
    echo "</td>";
}
echo "</table>";

View

<table>
    <?php foreach ($users as $user):?>
        <tr>
            <td><?=$user['name']?></td>
            <td><?=$user['age']?></td>
            <td><?=($user['age'] == 1 ? 'Female' : 'Male')?></td>
        </tr>
    <?php endforeach;?>
</table>

A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.

MVC

Model

The model is the central component of the pattern. It expresses the application's behavior in terms of the problem domain, independent of the user interface.[6] It directly manages the data, logic and rules of the application.

Let's ask Google and some friends

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

Calculator

but, but, but...

Controller

The third part or section, the controller, accepts input and converts it to commands for the model or view

Also

The controller is responsible for responding to the user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model

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 ...

but, but, but...

Command line

and

a WebSocket server

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

 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.

Does all that work for me?

  • A model of a calculator
  • 3 types of inputs (controllers)
    • ncurses
    • text
    • some sort of JSON or something
  • 3 types of output
    • ncurses
    • text
    • some sort of JSON or something

A perfect separation of concerns

What about the frameworks?

... this is for another talk.

Enlightenment

Enlightenment (to an extent)

Thank you!

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