Blog

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:

  1. The user interacts with the web page in some way, such as making selections in a form and clicking a submit button.
  2. The controller handles the input and routes the request to the appropriate action, which is usually a public method of the controller class.
  3. The controller notifies the model of the user's action, usually resulting in a change to the model's state.
  4. 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.
  5. The user interface (the web page) awaits a new interaction from the user, and the cycle begins again.

Figure below shows how the various MVC application components interact with each other to handle a user request and generate a response. mvcarchitectimg1The anatomy of an MVC request and response.

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.

ldn-pledgerwoodThis post is an excerpt from the online courseware for our MVC 4.0: Views and Models course written by expert Phil Ledgerwood.



Phil Ledgerwood

Philip Ledgerwood has been a software developer for fifteen years. He currently works primarily in .NET technologies producing custom software for organizations of all sizes. He has also done extensive training for those same organizations in both technical and business process topics. Philip is a strong advocate of Lean and agile software development and spends most of his time helping companies interested in the value those practices can bring to their development efforts. He does this through a combination of training and working "in the trenches" as a developer on these teams, keeping a hand in the academic side of emerging technology and practices while also directly applying it in real projects to bring real business value.


MVC

This course excerpt was originally posted November 29, 2013 from the online courseware MVC 4.0, Part 03 of 11: Views and Models by Phil Ledgerwood