The MVC Architecture

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 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.

Be Sociable, Share!

Leave a Reply

Your email address will not be published. Required fields are marked *