ASP. NET development, from Layer 2 to Layer 3 to object-oriented (2)

Source: Internet
Author: User

Continue the previous article ASP. NET development, from the second layer to the third layer, to the object-oriented http://www.cnblogs.com/insus/p/3822624.html. We learned how to change our program from Layer 2 to Layer 3. In this article, we will learn about Object-Oriented Programming in asp.net.

In reality, we should not directly create a BizBase class in. aspx. cs. The correct method is to create an object that has a direct relationship with. aspx. cs. Such as the Item object. According to the database table design, it has three fields, so we create three properties in this class:



Next we are respectively directed. aspx. several methods of cs, function re-refactoring, first look at Data_Binding (), move the line #44 to #49 in the Code to the Item class, and create a GetAll () function that returns the DataTable.

 

We can solve the problem mentioned just now, that is, in the Item class new BizBase class, rather than in. aspx. cs. Better coupling. In this way, the Data_Binding () method in. aspx. cs is restructured:



Here, we are new Item. The following describes how to rebuild and add records, and move some related code to the Item class. This method has two steps: Judgment and insertion:

The click event of the Add button in. aspx. cs can be reconstructed:

 
Now we start to reconstruct the program for event deletion to the Item category and create a Delete () method:

 

After the GridView event is deleted and reconstructed, it becomes like this:

 

This is fully implemented using objects. That is, the property and method of the object. You do not need to worry about how the specific property or method works. The encapsulation effect is achieved.

 
The last reconstruction is to obtain the scalar value. In the Item object, create a function GetDescriptionByItemNbr () that returns the string value, which involves two lines of code:

 

The commented-out code is the code originally in. aspx. cs.

 


After a series of moving operations and deleting all comments, a complete Item object is obtained:

 
The result was originally instantiated in. aspx. cs, and is now completely in the Item class.
Let's take a look at the. aspx. cs code. After deleting all the comments, there are still many places where we can see two instantiated codes:

 Item objItem = new Item();

And

InsusJsUtility js = new InsusJsUtility();


In this way, we can move them from the method or function to the page:

 

You can delete these two sentences in other methods. Final complete. aspx. cs code:



Later, the BizBase class basically does not need to be modified. A new table is added to the database. You can create a class similar to Item by using the table name. It includes property, method, function, and so on. In the webpage, the new object is instantiated directly.

This article demonstrates the source program (Access DB;. NET Framework 4.0 ;):
Http://download.cnblogs.com/insus/ASPDOTNET/Asp_OOP2.rar


Note: The article is too long to be written as a sequent. Please pay attention to it ......

Related Article

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.