自定义实体类简介的
2021-09-06 01:50 来源:璧山县养生网
对于在许多数据绑定控件提供的 OnItemDataBound 或 OnItemCreated 中执行处理的人来说,您可能会将 taItem 强制转换成 DataRowView。当绑定到自定义集合时,taItem 则被强制转换成自定义实体,在我们的示例中为 User 类:
\'Visual Basic .NET
Protected Sub r_ItemDataBound (s As Object, e As RepeaterItemEventArgs)
Dim type As ListItemType = emType
If type = ternatingItem OrElse
? type = em Then
Dim u As Label = CType(ndControl(\"userName\"), Label)
Dim currentUser As User = CType(taItem, User)
If Not sswordIsSecure(ssword) Then
reColor = d
End If
End If
End Sub
//C#
protected void r_ItemDataBound(object sender, RepeaterItemEventArgs e) {
ListItemType type = emType;
if (type == ternatingItem
? type == em){
Label ul = (Label)ndControl(\"userName\");
User currentUser = (User)taItem;
if (!sswordIsSecure(ssword)){
reColor = d;
}
}
}
管理关系
即使在最简单的系统中,实体之间也存在关系。对于关系数据库,可以通过外键维护关系;而使用对象时,关系只是对另一个对象的引用。例如,根据我们前面的示例,User 对象完全可以具有一个 Role:
\'Visual Basic .NET
Public Class User
Private _role As Role
Public Property Role() As Role
Get
Return _role
End Get
Set(ByVal Value As Role)
_role = Value
End Set
End Property
End Class
//C#
public class User {
private Role role;
public Role Role {
get {return role;}
set {role = value;}
}
}
或者一个 Role 集合:
\'Visual Basic .NET
Public Class User
Private _roles As RoleCollection
Public ReadOnly Property Roles() As RoleCollection
Get
If _roles Is Nothing Then
_roles = New RoleCollection
End If
Return _roles
End Get
End Property
End Class
//C#
public class User {
private RoleCollection roles;
public RoleCollection Roles {
get {
if (roles == null){
roles = new RoleCollection();
}
return roles;
}
}
}
维斯布鲁克两次单打成功 在这两个示例中,我们有一个虚构的 Role 类或 RoleCollection 类,它们就是类似于 User 和 UserCollection 类的其他自定义实体或集合类。
映射关系
真正的问题在于如何映射关系。让我们看一个简单的示例,我们希望根据 userId 及其角色来检索一个用户。首先,我们看一看关系模型:
498)this.style.width=498;\" border=0>
图 3:User 与 Role 之间的关系
这里,我们看到了一个 User 表和一个 Role 表,我们可以将这两个表都以直观的方式映射到自定义实体。我们还有一个 UserRoleJoin 表,它代表了 User 与 Role 之间的多对多关系。
然后,我们使用存储过程来获取两个单独的结果:第一个代表 User,第二个代表该用户的 Role:
CREATE PROCEDURE GetUserById(
@UserId INT
)AS
SELECT UserId, UserName, [Password]
FROM Users
WHERE UserId = @UserID
SELECT leId, R.[Name], de
FROM Roles R INNER JOIN
UserRoleJoin URJ ON leId = leId
WHERE erId = @UserId
最后,我们从关系模型映射到对象模型:
\'Visual Basic .NET
Public Function GetUserById(ByVal userId As Integer) As User
Dim connection As New SqlConnection(CONNECTION_STRING)
Dim command As New SqlCommand(\"GetUserById\", connection)
d(\"@UserId\", ).Value = userId
Dim dr As SqlDataReader = Nothing
Try
en()
dr = ecuteReader()
Dim user As User = Nothing
If ad() Then
user = PopulateUser(dr)
xtResult()
While ad()
d(PopulateRole(dr))
End While
End If
Return user
Finally
If Not dr Is Nothing AndAlso Not Closed Then
ose()
End If
spose()
spose()
End Try
End Function
//C#
public User GetUserById(int userId) {
SqlConnection connection = new SqlConnection(CONNECTION_STRING);
SqlCommand command = new SqlCommand(\"GetUserById\", connection);
d(\"@UserId\", ).Value = userId;
SqlDataReader dr = null;
try{
en();
dr = ecuteReader();
User user = null;
if (ad()){
user = PopulateUser(dr);
xtResult();
while(ad()){
d(PopulateRole(dr));
}
}
return user;
}finally{
if (dr != null !Closed){
ose();
}
spose();
spose();
}
}
User 实例即被创建和填充;我们转移到下一个结果/选择并进行循环,填充 Role 并将它们添加到 User 类的 RolesCollection 属性中。
台州妇科习惯性流产治疗哪家好济南男科治疗费用
血压高的症状有哪些症状
-
如何在小学打造一间现代科学教室?
都说生物科学普及教育是提升全民生物科学专业性、 新建国际化型发逾国家的基础 那么,幼稚园生物科学普及教育 对从小唤起和保护孩子的好奇心和求知欲 培养学生的生物科学精神和实践国际化能力 具有重要含...
-
美国将公布不可忽视纪要,美联储或加息100个基点,明天A股会大跌吗?
现今,A股2台自主性,早盘三大期货虽有发挥作用涨跌的观感,但在恐慌抛盘的坠地下,最终没能稳住,跌势一直道股价。两市南先于攀升数量居多,攀升的有1298只,攀升的有3325只。归纳原因,与这四点有关:1、...
-
YOOT TOY REBEL BEAR 叛逆小熊 胆量与疯子 爱因斯坦 毛绒玩偶
YOOT TOY发布REBEL BEAR蛮横小乌鸦天才与屠夫新作相对论毛绒公仔 蛮横乌鸦 天才与屠夫新作 商品品牌:YOOT TOY 商品品名:蛮横乌鸦天才与屠夫新作REBEL ALBERT相对论 商品订制:磁吸项链、磁吸胡子、茶壶、背包、透明...
-
领导之所以不提拔你,是因为你没有这3样两边,别再蠢了
在基本单位中如果你一切都是让他组织晋升你,你一定要拥有这三都为进去,他组织只会晋升,有气魄的人,一定会晋升软弱无能的人,当你还没收到他组织晋升的时候,一定要可先好好得道自己。 他组织之所以...
-
美股短线跳水,三大股指尽数转跌
铝媒体 App 5月20日第一时间,美股短线艺术体操,三大期货剩余转跌。截至目前,道指跌0.3%,标普500指数、纳指跌0.15%。马斯克跌超5%,英伟达跌超3%,雅虎跌近1%。急救护理哪个血糖仪比较可靠骨质疏松吃什么补的...
-
翡翠要养是不是却是新鲜感?这就是“人养玉,玉养人”的意义所在
听惯了园中的督导,因为这些都是有全人类的木本植物,如果说起珍珠要养是不是有点乐趣?说是珍珠就是石之美者,是一块经过地壳运动在一定的密度和压力中的形成的。所以珍珠是大其本质中的的诸城也是需要...