MVC is a software design pattern that separates an application into Model (data), View (display), and Controller (logic). Separation of concerns improves maintainability and testability, and it is adopted in Ruby on Rails, Django, Spring, and others.
MVC (Model-View-Controller) is a software architecture pattern first introduced by Trygve Reenskaug in Smalltalk in 1979. It separates an application into three core components to achieve Separation of Concerns and improves code reusability and maintainability.
The Model is responsible for the application's data and business logic. It includes interaction with the database, validation, and data processing rules. The View is responsible for the user interface and visually represents the Model's data. HTML, template engines, and front-end components fall into this category.
The Controller acts as an intermediary between the Model and the View. It receives user input, updates the Model, and selects the appropriate View to generate a response. It performs roles such as handling HTTP requests, routing, and checking authentication/authorization. Separating these three components allows each area to be developed and tested independently.
Most modern web frameworks adopt MVC or its variants (MVP, MVVM). Ruby on Rails popularized MVC with "Convention over Configuration," and Django (MTV: Model-Template-View), ASP.NET MVC, Spring MVC, and others are widely used. Front-end frameworks such as React and Vue focus on the View layer and handle part of MVC within the overall architecture.
π¬ Situation: New project architecture design meeting
PM"How are we going to structure the backend for the new project? I'd like a structure that team members can easily understand and collaborate on."
Backend"We plan to use the MVC pattern as the base, but add a Service layer to keep the Controller thin. The structure is: the Controller handles only requests and responses, business logic goes in the Service, and data access goes in the Model/Repository."
Frontend"So we just communicate with the REST API from the front end, right? Since we'll use React for the View layer, it seems the backend only needs to return JSON."
Backend"Right. Whereas in traditional MVC the View was a template engine, in our case the API response plays the role of the View, and React handles the actual screen. This structure is sometimes called an MVC + SPA architecture. It also becomes easier to write unit tests for each layer."
β οΈ Cautions
Fat Controller anti-pattern: Putting business logic in the Controller makes testing difficult and bloats the code. Introduce a Service layer so that the Controller handles only request/response processing and delegates business logic to the Service.
Excessive Model responsibilities: When the Model handles data access, business logic, and validation all at once, it becomes a God Object. Consider separating data access with the Repository pattern and distinguishing Domain objects from Entities.
Harmony with modern front-ends: In SPAs such as React and Vue, a traditional server-side View is not needed. Replace the V of MVC with JSON responses from the API server, and have the front end use a separate component architecture.