ASP. NET: custom object class

Source: Internet
Author: User

ASP. NET: Custom object class

What is a custom object?

Custom entities are the objects representing the business domain. Therefore, they are the foundation of the business layer.If you have a user authentication function (this example is used throughout this article), you may have user and role objects. E-commerce systems may have supplier and merchandise objects, while real estate companies may have house, room, and address objects. In yourCodeIn, the custom object is just some classes (the entity and the "class" are closely related, as they are used in OO programming ). A typical user class may be as follows:

 

'Visual basic. net declaration user information table class public class usere # region "fields and properties" defines various information in the table private _ userid as string private _ PWD as string private _ level as string private _ username as string private _ head as string 'Access public property userid () of The USERID field in the User table () as string get return _ userid end get set (value as string) _ userid = value end set end property 'access to the password field in the User table public property PWD () as string get return _ PWD end get set (value as string) _ Pwd = value end set end property 'access to the field level in the User table public property level () as string get return _ level end get set (value as string) _ Level = value end set end property 'access to the username field in the User table public property username () as string get return _ username end get set (value as string) _ username = value end set end property 'access public property head () in the User table () as string get return _ head end get set (value as string) _ Level = value end set end property end class

 

Why can we benefit from them?

The main benefit of using custom entities comes from the fact that they are fully controlled by you. Specifically, they allow you:

• Use oo technologies such as inheritance and encapsulation.

• Add custom behaviors.

For example, our user class can benefit from adding updatepassword functions to it (we may use external/practicalProgramFunctions perform such operations on datasets, but affect readability and maintainability ). In addition, they are strongly typed, which means we can get support for intelliisense:

Figure 1: User-class intelliisense)
Finally, because the custom object is of a strong type, it is not necessary to perform an error-prone forced conversion:

 

 
Dim userid as integer = user. userid 'and dim userid as integer =? Convert. toint32 (Ds. Tables ("users"). Rows (0) ("userid "))

 

Object link ing

As discussed above, one of the main challenges of this method is to process the Differences Between Relational Data and objects. Because our data is always stored in relational databases, we can only build a bridge between the two worlds. For the preceding user example, we may want to create a user table in the database as follows:

Figure 2: User Data View

Ing from this relational architecture to a custom object is very simple:

 

 

'Visual basic. netpublic function getuser (byval userid as integer) as userdim connection as new sqlconnection (connection_string) dim command as new sqlcommand ("getuserbyid", connection) command. parameters. add ("@ userid", sqldbtype. INT ). value = useriddim Dr as sqldatareader = nothingtryconnection. open () DR = command. executereader (commandbehavior. singlerow) if dr. read thendim user as new useruser. userid = Convert. tostring (DR ("userid") user. username = convert. tostring (DR ("username") user. password = convert. tostring (DR ("password") return userend ifreturn nothingfinallyif not Dr is nothing andalso not dr. isclosed thendr. close () end ifconnection. dispose () command. dispose () end tryend function // C # public user getuser (INT userid) {sqlconnection connection = new sqlconnection (connection_string); sqlcommand C Ommand = new sqlcommand ("getuserbyid", connection); command. parameters. add ("@ userid", sqldbtype. INT ). value = userid; sqldatareader DR = NULL; try {connection. open (); DR = command. executereader (commandbehavior. singlerow); If (dr. read () {user = new user (); User. userid = convert. toint32 (Dr ["userid"]); User. username = convert. tostring (Dr ["username"]); User. password = convert. tostring (Dr ["password"]); retur N user;} return NULL;} finally {If (Dr! = NULL &&! Dr. isclosed) {dr. Close () ;}connection. Dispose (); command. Dispose ();}}

 

We still set the connection and command object in the usual way, but then create a new instance of the user class and fill in the instance from datareader. You can still use dataset in this function and map it to your custom object, but the main advantage of dataset over datareader is that the former provides a disconnected view of data. In this example, the user instance provides a disconnected view so that we can use the datareader speed.

.

The above is my simple understanding. If there is any discrepancy, please do not forgive me.

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.