Asp.net Programming Mechanism

Source: Internet
Author: User

First, asp.net applications are structured in three layers: presentation layer-business layer-data access layer. This is a common mode, but I have added some processing between layers, it mainly refers to data transmission between the processing layer and the layer. The details of each layer are as follows.

The business layer is a manifestation of the business logic and has nothing to do with the presentation layer. It is generally a class that contains business data and various business operations. Generally, the business layer does not directly call the data access layer, but calls it through a data access service class AccessService. In this way, the data access layer must implement certain interface IAccess (operations include LOAD, INSERT, UPDATE, DELETE ). When a business class needs to LOAD data, first pack the class data (usually a parameter) into a package, and then call the LOAD method of AccessService to call the LOAD operation of IAccess at the data access layer, IAccess reads data from the database, makes a package, returns the data to AccessService, and returns the data to the business layer. After the business layer unpacks the data packet, the data is changed to the data at the business layer (that is, the attributes of the business class ). At this time, the business data is loaded. Then, perform the corresponding business operations, compress the data into a package and discard it to the AccessService for processing, and save it to the database.

Why add an AccessService between the business layer and the data layer to reduce the coupling between the layers, that is, there is no association between the business layer and the data layer, and the data layer is only responsible for keeping the data permanent, it has nothing to do with the specific business. To change the database type, you only need to change the data layer.

In addition, the data layer does not directly access the database, but uses a common DataServiceForSqlServer (Data Classification class of the SQL SERVER database), which many people have, that is, some basic operations, such as ExecuteNonQuery, ExecuteDataSet, and ExecuteReader.

Asp.net adopts the code separation method on the interface. Many switches from asp to asp.net are used to writing code to the aspx file, or some methods still adopt asp, which is not desirable. Asp.net adopts the ViewState mechanism to realize the continuation of the page state. As we all know, the WEB is stateless. That is to say, after a page is loaded, all the page objects will be discarded and the data will be returned to the browser in HTML format. When a browser sends a request, the server creates various objects based on the request, processes the objects, and returns the data in HTML format to the browser. Therefore, the page is stateless, the server does not know the status of the previous page. Asp.net uses VIEWSTATE to let the server know the status of the previous page. All the pages mentioned here are the same page. Asp.net generally places a function on the same page, while asp generally adopts a multi-page approach.

So how does asp.net realize the continuation of the state? This section describes the lifecycle of the ASP.net control (the PAGE class is also a control class ). When the browser sends the first request, the server does the following:

Init initialization (create control tree initialization)
LoadViewState: load the view status (because the view status is empty for the first time)
LoadPostData process the returned data (that is, the form data, because on the server, the control exists in WEBCONTROL, and the form data is converted to the control data)
Load loading (here you can write your own initialization work)
RaisePostDataChangedEvent sends a send-back change notification (a change event is triggered to respond to status changes between the current and previous sending requests)
RaisePostBackEvent handles the sending back event (processing the client event that causes the sending back and triggering the corresponding event on the server. For example, The onClick event of the button is handled at this time)
PreRender pre-rendering (any update is executed before the rendering output. If any update to WEBCONTROL or VIEWSTATE is called after this event, it will be lost)
SaveViewState: Save the current page status)
Render rendering (generate the output presented to the client, which is returned to the browser in HTML format, where the view State data is saved as a hidden form and named "_ VIEWSTATE ")
Dispose disposal (execute all final cleanup operations before the control is destroyed)
UnLoad uninstall

I spent a lot of time talking about the above, just to figure out the mechanism of asp.net.

Let's talk about the presentation layer. The presentation layer only performs simple data verification and time processing, instead of processing the business, it is processed by the business layer. During the first loading, you will create a page-related business object and bind the data to the webcontrol on the interface to load the data. There are two ways to save this business object as a view, modify the data, and issue the Save processing command before submitting it to the database; in addition, after issuing the Save processing command, you can create the original business object and submit the modified data to the database. The former can reduce the number of database accesses, because the object is stored in VIEWSTATE (there is a serialization problem here), each time the server and the client round-trip data is large.

A business event in the presentation layer can generally be a business operation in the business layer, so that the presentation layer does not care how the specific business is implemented. I also made a processing in the middle, that is, assigning the form value to the business class attribute (that is, converting the value into business data, there is also a problem of data type conversion ). I implemented the conversion through the service of BinderReflectService. How is it implemented? There is a BinderMappingCollection object in the business class, which records the attribute names of the business class and the names of related controls, binderReflectService automatically retrieves control data and assigns values to the specified attribute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.