This article corresponds to the web representation mode cluster. The first half of the article describes the architecture, design and ASP.NET implementation of the MVC mode, and in a more complex system, then presentation Page Controller and Front. Controller as an addition of MVC implementation, finally, two other two modes of the web representation mode cluster: Intercepting Filter (Filter) and Page Cache mode.
"The first work of the architecture designer is often more concise and clean. He knows that he doesn't understand the work, so he is careful to design it. When he designs the first work, it will make multiple modifications and Grunge. These will be used to "next" ... this second system is the most dangerous system he has designed ... General Trend is that when designing the second system, it will be used in the first work. Be careful to put all the ideas and modifications on one side, resulting in the design of the head. "
-Frederick P. Brooks, Jr. Posted in 1972 The Mythical Man Month (Moon Myth).
The first system established on the web is a simple HTML page that is simply linked together to share documents between the dispersed groups. As the user's usage increases, the dynamic web page entered by the user is increasingly common. Early dynamic pages are typically written in the form of a universal gateway interface (CGI) script. These CGI scripts include not only to determine what contents should be displayed when responding to user input, but also generates HTML. With the increase in more complex logical needs, the demand for richer and more vivid representations is also increased. This increased complexity brings challenges to the CGI programming model.
Soon, the page development means (such as ASP and JSP) appear. These new methods allow developers to simplify the programming model in the HTML side of the developer. When these embedded script applications become more complicated, developers want to separate business logic with representations logically on the page level. To adapt to this request, there is a tag library with help objects and code hidden page policies. Then, a fine frame with dynamically configured site navigation and command scheduling programs appear, but all this is at the expense of increasing complexity. Suppose now has a large number of web represents an option, how to choose the appropriate web to indicate design policies for your application?
Is there a design strategy to adapt to all the situation? Unfortunately, in software design, eliminating excessive redundancy and excessive complexity is a competitive demand, it is difficult to truly do balance between the two. You can start designing work from a simple page that contains embedded scripts, but fast business logic will continue to repeat in each file, resulting in difficulties in maintenance and expansion. By moving the logic into a set of collaborative components, redundancy can be eliminated, but this will increase the complexity of the solution. On the other hand, your design work can be started from the design of the design to provide a flag, dynamic configuration, and command scheduling program, but though can eliminate redundant code, but it will greatly increase the complexity of the system, and this is usually unnecessary.
How to consider all aspects of demand, propose a Web representation strategy for our most suitable application? This requires a choice between complex solutions (support for future changes) and simple solutions (satisfying current requirements), in principle, appropriate increase in cost, and excessive increase cost is not available. . So, less nonsense, we will start from "simple".
MVC (Model - View - Control)
Many computer systems are used to retrieve data from the data store and display it to the user. After the user changes the data, the system stores the update content to the data store. Because the critical information flow occurs between data storage and user interfaces, you may tend to tie the two parts to reduce encoding and improve application performance. However, this seemingly natural method has some big problems. One problem is that changes in user interfaces are often more frequent than changes in data storage systems. Another problem with two parts of the data and user interfaces together is that business applications tend to enter other business logic for far more data transfer features. How to make the web application user interface function to modrew so that you can easily modify each part alone? Model-View-Controller is this mode that implements the separation of functional modules and display modules, making applications more maintainable, scalable, portable, and reused, it was initially Trygve Reenskaug in the 1970s. The last frame is developed by the SmallTalk platform [Fowler03], and there is a very mature pattern that has been formed so far.
MVC solution
Model-View-Controller (MVC) mode is based on user input, dividing the domain modeling, display, and operations into three independent classes [Burbeck92]:
model. The model is used to manage behavior and data of the application domain, and respond to requests issued by its status information (usually from view), and will also respond to the command to change the status (usually from the controller).
view. The view is used to manage information.
Controller. The controller is used to interpret the user's mouse and keyboard input to notify the model and / or the view for the corresponding changes.
Figure 1, a structural relationship between these three objects
Views and controllers are dependent on the model. However, the model does not depend on the view, nor does it depend on the controller. This is one of the main advantages of separation. Such separation allows the model to establish and test in the case of a visual representation function. In many fat client applications, the separation of the view and the controller is secondary, in fact, many user interface frames implement the role as an object. On the other hand, in a web application, the separation of the view (browser) and the controller (handling the HTTP request) is well defined.
Model-View-Controller is an underlying design pattern for separating user interface logic and business logic. Unfortunately, the popularity of this mode has led to many erroneous descriptions. Especially in different contexts, the term "controller" has been used to refer to different things. Fortunately, the emergence of web applications has helped eliminate some unclearness, because the separation of the view and the controller is so obvious.
MVC variant
In Application Programming In Smalltalk-80: How To Use Model-View-Controller (MVC) [Burbeck92], Steve Burbeck describes two variations of MVC: passive models and active models.
The passive model will be used when a controller operates a model in an exclusive manner. The controller will modify the model, then notify the view: The model has changed, and it should be refreshed (see Figure 2). The model in this case is completely independent of the view and controller, which means the model cannot report its status changes. The HTTP protocol is an example of this scheme. The browser does not get an easy way to get an asynchronous update from the server. The browser displays the view and responds to the user input, but it does not detect data changes on the server. If the user explicitly requests refresh, it will be inquiry if the server has changed.
Figure 2, the behavior of the passive model
The active model will be used when the model changes the status without involving the controller. This may happen when other resources are changing data and changes must be reflected in the view. Taking the display quotation machine as an example. You receive stock data from the external source and want to update the view (for example, quotation machine data area and warning window) when stock data changes. Since only the model detects changes to its internal state (when these changes occur, the model must be notified to refresh the display. However, a purpose of using the MVC mode is to make the model independent of the view. If the model must notify the view, you will re-brought the dependency that you want to avoid. Fortunately, OBServer mode [gamma95] provides this mechanism: reminding other objects to pay attention to the status of the status, without led to dependence on these objects. Each view implements the OBServer interface and registers to the model. The model will track a list of all observers made by subscription changes. When the model changes, the model will traverse all registered viewers and inform them. This method is often referred to as "release-subscription". The model does not require specific information about any view. In fact, the controller must do all the work that needs to implement the OBServer interface without the notification controller (for example, enabling or disabling the menu option). For situations where there are many views, it is meaningful to define a plurality of subjects, where each body describes a specific type of model change. Each view can then subscribe to the change type related to the view. Figure 3 shows the structure of the active MVC using OBServer, and how the viewer is separated from the direct reference view.
Figure 3, using the viewer to separate the model and view in the active model
Figure 4 illustrates how the OBServer notifies the view when the model changes. It is a pity that in the unified modeling language (UML) sequence diagram, there is no good way to demonstrate the separation of the model and the view because the figure shows an object's instance instead of the class and interface.
Figure 4, behavior of the active model
MVC ASP.NET implementation
To explain how to implement Model-View-Controller mode in ASP.NET, the benefits of separating models, views, and controller roles in software, the following example reconstructed a single page solution without separating all three roles To separate these three roles. The sample application is a web page with a drop-down list (shown below below), which shows a record stored in the database.
With the Microsoft Visual Studio (R) .NET development system, you can easily divide the representation (view) code from the Model-Controller code. Each ASP.NET page has a mechanism that allows the method to be called from the web page to be implemented in separate classes. This mechanism is provided by Visual Studio .NET, there are many advantages, such as Microsoft IntelliSense (R) technology. When you use the code hidden function to implement a web page, you can use IntelliSense to display the list of available methods of the objects used in the page backed by the page. IntelliSense does not apply to .aspx pages. At the same time, in order to show the separation of Model-Controller, DatabaseGateway is extracted for database operation, so that the complete separation of the three is achieved.
view
<% @ Page language = "c #" codebehind = "solution.aspx.cs" autoeventwireup = "false" inherits = "solution"%>