ASP.NET:自定义实体类
ASP.NET:自定义实体类
什么是自定义实体?
自定义实体是代表业务域的对象,因此,它们是业务层的基础。如果您有一个用户身份验证功能(本文通篇都使用该示例进行讲解),您就可能具有 User 和 Role 对象。电子商务系统可能具有 Supplier 和 Merchandise 对象,而房地产公司则可能具有 House、Room 和 Address 对象。在您的代码中,自定义实体只是一些类(实体和“类”之间具有非常密切的关系,就像在 OO 编程中使用的那样)。一个典型的 User 类可能如下所示:
[vb]
'Visual Basic .NET
声明User信息表类
Public Class UserE
#Region "Fields and Properties"
定义表中各种信息
Private _UserID As String
Private _PWD As String
Private _Level As String
Private _UserName As String
Private _Head As String
'对User表中字段UserID的存取
Public Property UserID() As String
Get
Return _UserID
End Get
Set(value As String)
_UserID = value
End Set
End Property
'对user表中字段密码的存取
Public Property PWD() As String
Get
Return _PWD
End Get
Set(value As String)
_PWD = value
End Set
End Property
'对User表中字段Level的存取
Public Property Level() As String
Get
Return _Level
End Get
Set(value As String)
_Level = value
End Set
End Property
'对user表中字段UserName的存取
Public Property UserName() As String
Get
Return _UserName
End Get
Set(value As String)
_UserName = value
End Set
End Property
'对User表中字段Head的存取
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
声明User信息表类
Public Class UserE
#Region "Fields and Properties"
定义表中各种信息
Private _UserID As String
Private _PWD As String
Private _Level As String
Private _UserName As String
Private _Head As String
'对User表中字段UserID的存取
Public Property UserID() As String
Get
Return _UserID
End Get
Set(value As String)
_UserID = value
End Set
End Property
'对user表中字段密码的存取
Public Property PWD() As String
Get
Return _PWD
End Get
Set(value As String)
_PWD = value
End Set
End Property
'对User表中字段Level的存取
Public Property Level() As String
Get
Return _Level
End Get
Set(value As String)
_Level = value
End Set
End Property
'对user表中字段UserName的存取
Public Property UserName() As String
Get
Return _UserName
End Get
Set(value As String)
_UserName = value
End Set
End Property
'对User表中字段Head的存取
Public Property Head() As String
Get
Return _Head
End Get
Set(value As String)
_Level = value
End Set
End Property
End Class
为什么能够从它们获益?
使用自定义实体获得的主要好处来自这样一个简单的事实,即它们是完全受您控制的对象。具体而言,它们允许您:
• 利用继承和封装等 OO 技术。
• 添加自定义行为。
例如,我们的 User 类可以通过为其添加 UpdatePassword 函数而受益(我们可能会使用外部/实用程序函数对数据集执行此类操作,但会影响可读性/维护性)。另外,它们属于强类型,这表示我们可以获得 IntelliSense 支持:
图 1:User 类的 IntelliSense( IntelliSense翻译为智能感应功能 )
最后,因为自定义实体为强类型,所以不太需要进行容易出错的强制转换:
[vb]
Dim UserId As Integer = User.UserId'
与
Dim UserId As Integer = ? Convert.ToInt32(ds.Tables("users").Rows(0)("UserId"))
Dim UserId As Integer = User.UserId'
与
Dim UserId As Integer = ? Convert.ToInt32(ds.Tables("users").Rows(0)("UserId"))
对象关系映射
正如前文所讨论的那样,此方法的主要挑战之一就是处理关系数据和对象之间的差异。因为我们的数据始终存储在关系数据库中,所以我们只能在这两个世界之间架起一座桥梁。对于上文的 User 示例,我们可能希望在数据库中建立一个如下所示的用户表:
图 2:User 的数据视图
从这个关系架构映射到自定义实体是一个非常简单的事情:
[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();
}
}
我们仍然按照通常的方式设置连接和命令对象,但接着创建了 User 类的一个新实例并从 DataReader 中填充该实例。您仍然可以在此函数中使用 DataSet 并将其映射到您的自定义实体,但 DataSet 相对于 DataReader 的主要好处是前者提供了数据的断开连接的视图。在本例中,User 实例提供了断开连接的视图,使我们可以利用 DataReader 的速度
。
以上是我的简单理解,如有出入,请勿见谅。