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 your code, user-defined entities are just some classes (entities and "classes" are closely related, as they are used in OO programming ). A typical User class may be as follows:
[Vb]
'Visual Basic. NET
Declare User information table class
Public Class UserE
# Region "Fields and Properties"
Define 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 The UserID field in the User table
Public Property UserID () As String
Get
Return _ UserID
End Get
Set (value As String)
_ UserID = value
End Set
End Property
'Access the field password 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 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 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 the field Head in the User table
Public Property Head () As String
Get
Return _ Head
End Get
Set (value As String)
_ Level = value
End Set
End Property
End Class
'Visual Basic. NET
Declare User information table class
Public Class UserE
# Region "Fields and Properties"
Define 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 The UserID field in the User table
Public Property UserID () As String
Get
Return _ UserID
End Get
Set (value As String)
_ UserID = value
End Set
End Property
'Access the field password 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 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 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 the field Head in the User table
Public Property Head () 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 the UpdatePassword function to it (we may use external/utility functions to perform this operation on the dataset, but it may affect readability/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:
[Vb]
Dim UserId As Integer = User. userid'
And
Dim UserId As Integer =? Convert. ToInt32 (ds. Tables ("users"). Rows (0) ("UserId "))
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:
[Vb]
'Visual Basic. NET
Public Function GetUser (ByVal userId As Integer) As User
Dim connection As New SqlConnection (Connection_String)
Dim command As New SqlCommand ("GetUserById", Connection)
Command. Parameters. Add ("@ UserId", SqlDbType. Int). Value = UserId
Dim dr As SqlDataReader = Nothing
Try
Connection. Open ()
Dr = command. ExecuteReader (CommandBehavior. SingleRow)
If dr. Read Then
Dim user As New User
User. UserId = Convert. ToString (dr ("UserId "))
User. UserName = Convert. ToString (dr ("UserName "))
User. Password = Convert. ToString (dr ("Password "))
Return User
End If
Return Nothing
Finally
If Not dr is Nothing AndAlso Not dr. IsClosed Then
Dr. Close ()
End If
Connection. Dispose ()
Command. Dispose ()
End Try
End Function
// C #
Public User GetUser (int userId ){
SqlConnection connection = new SqlConnection (CONNECTION_STRING );
SqlCommand command = 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 user = new User ();
User. UserId = Convert. ToInt32 (dr ["UserId"]);
User. UserName = Convert. ToString (dr ["UserName"]);
User. Password = Convert. ToString (dr ["Password"]);
Return user;
}
Return null;
} Finally {
If (dr! = Null &&! Dr. IsClosed ){
Dr. Close ();
}
Connection. Dispose ();
Command. Dispose ();
}
}
'Visual Basic. NET
Public Function GetUser (ByVal userId As Integer) As User
Dim connection As New SqlConnection (Connection_String)
Dim command As New SqlCommand ("GetUserById", Connection)
Command. Parameters. Add ("@ UserId", SqlDbType. Int). Value = UserId
Dim dr As SqlDataReader = Nothing
Try
Connection. Open ()
Dr = command. ExecuteReader (CommandBehavior. SingleRow)
If dr. Read Then
Dim user As New User
User. UserId = Convert. ToString (dr ("UserId "))
User. UserName = Convert. ToString (dr ("UserName "))
User. Password = Convert. ToString (dr ("Password "))
Return User
End If
Return Nothing
Finally
If Not dr is Nothing AndAlso Not dr. IsClosed Then
Dr. Close ()
End If
Connection. Dispose ()
Command. Dispose ()
End Try
End Function
// C #
Public User GetUser (int userId ){
SqlConnection connection = new SqlConnection (CONNECTION_STRING );
SqlCommand command = 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 user = new User ();
User. UserId = Convert. ToInt32 (dr ["UserId"]);
User. UserName = Convert. ToString (dr ["UserName"]);
User. Password = Convert. ToString (dr ["Password"]);
Return 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.