The .NET MVC Pattern

Wikipedia attributes the Model-View-Controller architectural pattern to Trygve Mikkjel Heyerdahl Reenskaug, who developed it in 1979 while working on Smalltalk at Xerox PARC.

The key concept in this pattern is that you start with a model—that is, a representation of the problem domain. The model includes the state of the application and its data; it focuses on the structure of the data and how it will be manipulated.

The second key concept in the MVC pattern is the view, which is how the model is resented to the user (i.e., the user interface). The view typically includes controls ith which the user interacts (drop-down lists, buttons, etc.).

The third and final key concept is (you guessed it!) the controller, which responds to ser actions (and other events) and mediates the interaction between the model and he view, possibly modifying one and/or the other. For example, pressing a button ay cause the controller to send a message to the model, thereby changing the state f the model. This may in turn cause the controller to send another message, this time back to the view, updating the view to represent the new state of the model.

The ASP.NET MVC Framework
In most of .NET (including some of .NET 3.5), MVC is not easily implemented, as the controller’s responsibilities are spread out among the event handlers, the framework, the CLR, and the operating system. Instead, Microsoft has emphasized the n-tier approach, which more clearly separates the model into business objects and persistence objects (which it provides in the form of the new ADO.NET class libraries, like the Entity Framework).

However, Microsoft now provides an MVC Framework for ASP.NET as an optional feature. The MVC Framework maps the Model-View-Controller design pattern onto the .NET Framework, creating a very powerful synergy.

The ASP.NET MVC Framework adds templates for Visual Studio that make it easy to create an MVC web application. When you create an MVC application, Visual Studio creates two projects: the first is a web project, and the second is a testing project specifically created to enable you to verify that the web project works as expected.

Within the web project, Visual Studio creates three folders, conveniently named
/Controllers, /Models, and /Views.

Controller classes and action methods
The MVC Framework maps URL requests directly to controller classes, by default.
Controllers are responsible for handling incoming page requests, managing user input, and executing the underlying logic.

A controller class can respond to URL requests by overriding the Execute( ) method of its base class and examining the incoming URL to see what is being requested. An easier option, however, is to define action methods on the subclassed controller. The base class will then automatically route the requests to the correct method, based on the rules of your application. Incoming URL parameters are typically accessed as parameter arguments to the action methods.

Model classes
In traditional MVC, the model is the component responsible for maintaining state. With ASP.NET, state is typically persisted in a database. The model classes of the ASP.NET MVC Framework work well with ADO.NET, LINQ, or any other implementation you may choose.

View classes
The application logic is encapsulated in the controller classes and the persistence logic in the model classes. This leaves the view classes free to focus on the presentation logic.
Typically, controller action methods will handle incoming web requests, use the incoming parameter values to execute the application logic code, talk to the model to retrieve data as needed, and then select view objects to render results to the requester.

Source of Information : OReilly Programming dot NET 3.5


Subscribe to Developer Techno ?
Enter your email address:

Delivered by FeedBurner