MVC

Modelo–Vista–Controlador

Escrito por Darjus Kazakevic · revisado el

Keep state, display and input handling in three separate places.

MVC splits user-interface code into three roles. The model holds the data and the business rules. The view displays the model. The controller receives user input, such as a click or an HTTP request, and turns it into calls on the model.

There are two common forms. In desktop MVC the view watches the model and redraws when it changes. In web MVC (Rails, Django, Spring MVC, Laravel) each request runs a controller action, which loads the model and renders a template. In both, the business rules belong in the model.

En esencia — Split UI code into a model that holds state and rules, views that display it, and controllers that turn user input into model changes.

Cuándo recurrir a él

Recurre a él cuando

  • A server-rendered web app: each request runs an action and renders a page
  • Several views must show the same state and stay consistent
  • A team that wants a well-known layout (Rails, Django, Spring MVC)

Reconsidéralo cuando

  • A data-binding UI framework (SwiftUI, Jetpack Compose): MVVM fits it
  • Screen logic must be unit-tested without rendering: MVP fits better
  • A JSON-only API with no views: handlers plus use cases are enough

The three roles

Model
The data and the rules that guard it: a cart, its lines, and the rule that a quantity must be at least 1. It knows nothing about screens or HTTP.
View
Displays the model: an HTML template, a desktop window or a JSON serializer. It reads the model but never changes it or makes business decisions.
Controller
Receives input and turns it into model calls. In a web app it is usually a class with one action per route: parse the request, call the model, choose a view.
Desktop vs. web MVC (two shapes, one name)
In desktop MVC the view subscribes to the model and redraws on every change. Web MVC drops the subscription: the controller renders a fresh view for each request. Same roles, different arrows.

Reglas de diseño

Keep business rules in the model
A controller turns input into model calls and nothing more. When it validates, computes totals or branches on business state, move that logic into the model.
Never let the model know about views
The model may announce “I changed”, but it never formats, renders or names a view. If it does, you cannot add a second view or test the rules without a UI.
Keep controllers thin
Aim for a few lines per action: parse input, call the model, pick a view. A 200-line action means model methods are missing.
Keep queries out of views
A template that loads data inside a loop runs one query per row (the N+1 problem). Load the data in the controller and pass it to the view.

Errores habituales

  • Fat controllers: validation and pricing in the action. The same rule is then copied into the API controller and the admin controller, and the copies drift.
  • Models that are only data bags, so every rule ends up in controllers.
  • Queries inside view templates. A page with 50 rows runs 51 queries.