The MVC Architecture
Excerpt by Phil Ledgerwood | November 29, 2013
As the name implies, there are three primary components of an MVC framework: one or more models, views, and controllers. These components define the response cycle of a user interaction with an application.
- Model: A domain-specific set of classes that provide an interface into the data, implement domain-specific business rules, and reflect application state. The model may or may not wrap a persistent data store such as SQL Server or other database server. Usually the model will consist of a data access layer or some kind of object-relational tool such as LINQ to SQL, Entity Framework, or NHibernate.
- View: The application's user interface, which serves to render the model in a form that the user can interact with. An application will typically have multiple views associated with a single model, each used for different purposes. In a web application, the view is generally defined by HTML and contains no code or logic, other than what is necessary to display the view to the user and allow the user to interact with the view.
- Controller: Receives requests from the user and initiates a response, interacting with the model as necessary. The controller also handles the overall application flow. In a web application, the controller usually responds to HTTP GET or POST inputs, hands the request over to the model that implements the business rules, and then selects the view subsequently displayed to the user.
The user's interaction with a web-based MVC application generally follows this pattern:
- The user interacts with the web page in some way, such as making selections in a form and clicking a submit button.
- The controller handles the input and routes the request to the appropriate action, which is usually a public method of the controller class.
- The controller notifies the model of the user's action, usually resulting in a change to the model's state.
- The controller then selects a view, which queries the model in order to generate a new web page. In essence, the controller tells the view to generate itself, based on the current model state.
- The user interface (the web page) awaits a new interaction from the user, and the cycle begins again.
If you're familiar with ASP.NET Web Forms development, notice that there are no page events involved in responding to a user request. This makes for a much cleaner, simpler, and more maintainable architecture for applications.