MVC and OOP

by Gisle Hannemyr

This chapter introduces the general MVC paradigm and OOP. It also contains notes about how to apply this in Drupal

Table of contents

Introduction

The MVC (model-view-controller) user interface paradigm was first introduced by Trygve Reenskaug around 1978 when he was a visiting scientist at Xerox Palo Alto Research Laboratory (PARC). It was used there as part of the Smalltalk-80 programming system. However, MVC was conceived as a general solution to the problem of supporting the user's mental model of an information domain and to enable the user to inspect and change the information. The paradigm is applicable to all sort of information system user interfaces. It is tightly coupled to OOP (Object Oriented Programming)..

The tool

The main purpose of MVC is to provide a tool that bridges the gap between the human user's mental model of an information domain and the computer model of the same information domain. This tool consists of a view-controller pair.

The ideal information system based upon the MVC paradigm provides the user with the illusion of seeing and manipulating the domain information directly. The figure below illustrates this idea.

gnome

The MVC paradigm divides the information system into three kinds of components (model, view and controller). Outside the information system is the human user that uses the controller as a means to manipulate the model, and sees the model as it is presented to him or her through a view. The three different components interacts as follows:

In the scheme described above, tools (i.e. controller-view pairs) have exactly one model, but a model can have one or several tools associated with it. To maximise data encapsulation and thus code reusability, views and controllers need to know about their model explicitly, but models should not know about their views and controllers. A change in a model's state is often triggered by a controller connecting a user action to a message sent to the model. This change should be reflected in all of its views, not just the view associated with the controller that initiated the change.

The standard interaction cycle in the MVC paradigm is that the user takes some input action and the active controller notifies the model to change itself accordingly. The model carries out the prescribed operations, possibly changing its state, and broadcasts to its dependents (views and controllers) that it has changed, possibly telling them the nature of the change. Views can then inquire of the model about its new state, and update their display if necessary. Controllers may change their method of interaction depending on the new state of the model.

Example

Context
An enterprise handles a number of different business functions a.k.a. domains. Examples are design, materials management, planning, control, and finance.
Problem
The enterprise needs to support such domains with an integrated information system.
Solution
Create separate domain services for each of the different business domains. Each of these services should be tightly integrated internally, e.g., through a common data-base or through tightly coupled interacting services. Integration between domains will be through mechanisms that are outside the domain services

Drupal

In Drupal 7, the function t() was a global function that made contents translateable. In Drupal 8 and 9, it is provided as a class method ($this->t()).

You should still use the global function in procedural code (e.g. hooks). But if you extend a Drupal base class like a controller or a plugin the translate function is provided as class method ($this->t()) and you should use it. This makes your code testable.

PHPUnit testing allows for tests to be written to confirm everything works, so that anytime code is changed, the tests can be run to ensure nothing has been broken. The relevance of this is that PHPUnit testing tests only against a single class (aka a unit), meaning that core is not bootstrapped for these tests. Global functions such as t() therefore do not exist, and they will throw an error, preventing the tests from being run.

Source: D.SA.

Final word

[TBA]


Last update: 2020-05-31 [gh].