Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

State management in ASP.NET MVC

State management in ASP.NET MVC is an interesting throwback to the core concept of statelessness in HTTP—wherein there is no default support for state persistence. Compare this to ASP.NET Web Forms, which by default use ViewState to store the state for each control.

There are many benefits associated with stateless pages, or when state is maintained only on the client browser, via a cookie for example:
  • Sites can easily scale to a cluster of servers in a server farm without having to worry about maintaining session state.
  • Debugging problems produced by session expiry will not be an issue.
  • Cached pages can be put to better use, as most of the page content might stay the same in the absence of session state.
  • A session is easy to recover if the server crashes when state is persisted on the client rather than the server.
The MVC team clearly had the above in mind when creating a default, stateless architecture.

Though there is no default support for state persistence in MVC, developers can use several options to enable state persistence across page post-backs:
  • Session state
  • ASP.NET cache
  • Cookies
Each of the above has its own pros and cons depending on the scale of the deployment. By letting developers build their own persistence logic, ASP.NET MVC encourages developers to follow best practices in ensuring minimal memory and bandwidth usage which has been sorely lacking in Web Forms considering the common abuse of ViewState for caching large, unnecessary information.

The MVC Programming Model

ASP.NET MVC is a framework for building web applications using a MVC (Model View Controller) design
  • The Model represents the application core (business layer).
  • The View displays the data (the display layer).
  • The Controller handles the input (input control).
The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as Master Pages and Membership–Based Authentication. The MVC framework is defined in the System.Web.Mvc namespace.

The MVC model also provides full control over HTML, CSS, and JavaScript.

The MVC model defines web applications with 3 logical layers:
  • The business layer (Model logic)
  • The display layer (View logic)
  • The input control (Controller logic)

Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve data (and store data) from a database.

Views are the parts of the application that handles the display of the data (User Interface). Most often the views are created from the model data. The view in MVC in dumb. It just receives data.

Controller is the part of the application that handles user interaction. Typically controllers read data from a view, control user input, and send input data to the model.

In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. The MVC pattern helps us to create applications that separate the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements. The pattern specifies where each kind of logic should be located in the application.

The MVC separation helps you manage complex applications, because you can focus on one aspect a time. For example, you can focus on the view without depending on the business logic. It also makes it easier to test an application. The MVC separation also simplifies group development. Different developers can work on the view, the controller logic, and the business logic in parallel.