当前位置:网站首页>DataTable数据转换为实体
DataTable数据转换为实体
2022-07-07 18:44:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
我们在用三层架构编写软件时,常常会遇到例如以下问题,就是三层之间的參数传递问题:假设我们在D层查询出数据是DataTable类型的,那么我们在B层甚至U层使用这条数据时,就要用DataTable类型来回传递了,不管什么情况,我们都会不可避免的要填写读取的字段。比如我们须要使用第一条记录的的某个字段。代码须要这样写:mrc.count(*)rows(*).这样写的坏处有非常多:
1、easy写错,而且编译器是检查不出来的;
2、我们须要具体的了解数据库的结构;
3、 不符合面向对象编程思想。
这个问题研究了非常长时间,查找了无数的资料,最终找到解决方法了。将DataTable数据转化成单个的实体类。然后将这些实体类放到泛型集合中。
结果图例如以下:
实体类是数据库的映射,每一条记录相应一个实体。实体的属性相应每一条记录的字段,而且是一一相应的。我们这里是把查询到的每一条数据都作为一个实体提取出来,然后将这些实体存放到泛型集合中。这样我们在使用数据的时候仅仅要知道属性就能够,使用代码例如以下:List.(items).property。这样。是不是简化了代码,降低了工作量,也降低了错误率。
那么。是怎样用代码实现的呢?首先是实体类,这里,我们如果数据库中仅仅有两个字段用户民和password:
Public Class User
Public UserName As String
Public PassWord As String
Public Property _username() As String
Get
Return UserName
End Get
Set(value As String)
UserName = value
End Set
End Property
Public Property _password() As String
Get
Return PassWord
End Get
Set(value As String)
PassWord = value
End Set
End Property
End Class这里。我是用了一个ModelHelper类来实现这个功能。由于这是关于參数的类,将这个类放到了Model层。代码例如以下:
Imports System.Collections.Generic '命名空间
Imports System.Reflection '引入反射:便于使用propertyInfo
''' <summary>
''' 实体转换类。此类用于将数据表格转换为实体集合
''' </summary>
''' <remarks></remarks>
Public Class ModeHelper
Public Function convertToList(Of T As {New})(ByVal dt As DataTable) As IList(Of T)
'将dataTable转化为泛型集合
'1convertToList(Of T As {New})这里的new是用来约束參数T的。否则实例化时回出现错误
'2List后边的參数总是(of +)类型
Dim myList As New List(Of T) '定义返回值集合
Dim myType As Type = GetType(T) '定义实体类类型名
Dim dr As DataRow '定义行集
Dim tempName As String = String.Empty '定义一个暂时变量,用来存放
'数据表总是一个二维表格,须要使用数组:dr和pr,dt表示sqlhelper返回结果
For Each dr In dt.Rows '遍历DataTable全部记录
Dim myT As New T '定义一个实体对象
Dim Propertys() As PropertyInfo = myT.GetType().GetProperties() '定义属性集合,获得公共属性
Dim pr As PropertyInfo
For Each pr In Propertys '遍历DataTable全部字段
tempName = pr.Name '将属性名赋值给暂时变量
'检查Datatable是否包括此列(列名==对象属性名)
If (dt.Columns.Contains(tempName)) Then '将此属性与datatable的列名比較,查看datatable是否包括此列
If (pr.CanWrite = False) Then '推断此属性是否有Setter
Continue For '继续执行
End If
Dim value As Object = dr(tempName) '定义一个对象性的变量来保存列的值
If (value.ToString <> DBNull.Value.ToString()) Then '假设非空,则付给对象的属性
pr.SetValue(myT, value, Nothing) '执行期间,通过反射,动态的訪问一个对象的属性
End If
End If
Next
myList.Add(myT) '加入到集合
Next
Return myList '返回结果
End Function
End Class下边是D层的调用代码:
Public Function SelectUsers1(user ) As List(Of Charge.Model.User)
Dim mrc as dataTable '如果mrc是从数据库中查询出来的DataTable数据表
Dim myList As List(Of Charge.Model.User) '定义一个集合用来返回转化后的实体集合
Dim mHelper As New Charge.Model.ModeHelper '实例化一个实体转换类
myList = mHelper.convertToList(Of Charge.Model.User)(mrc) '调用实体转换类的方法,转换数据
Return myList '返回结果
End Function在这里,我们仅仅讨论将DataTable数据类型转换问题。其它问题不讨论,一切以如果作为前提,參考代码需慎重。
到眼下为止,这些代码攻克了我遇到的问题。可是细致思考一下。这里一个实体相应数据库的一条记录,也就是说。每个表都会有一个实体类或者泛型集合来相应,可是假设是多个表联合查询。该怎样解决呢?眼下我还没有解决问题,留待以后解决。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116450.html原文链接:https://javaforall.cn
边栏推荐
- 反诈困境,国有大行如何破局?
- 理财产品要怎么选?新手还什么都不懂
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- 万字总结数据存储,三大知识点
- Alibaba cloud award winning experience: how to mount NAS file system through ECS
- 【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
- I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
- Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
- Introduction to referer and referer policy
猜你喜欢

VMWare中虚拟机网络配置

C language helps you understand pointers from multiple perspectives (1. Character pointers 2. Array pointers and pointer arrays, array parameter passing and pointer parameter passing 3. Function point

Intelligent software analysis platform embold

Details of C language integer and floating-point data storage in memory (including details of original code, inverse code, complement, size end storage, etc.)

CodeSonar网络研讨会

Apifox 接口一体化管理新神器
SQL注入报错注入函数图文详解

【OpenCV 例程200篇】223. 特征提取之多边形拟合(cv.approxPolyDP)

ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

机械臂速成小指南(十二):逆运动学分析
随机推荐
使用camunda做工作流设计,驳回操作
万字总结数据存储,三大知识点
如何挑选基金产品?2022年7月份适合买什么基金?
【函数递归】简单递归的5个经典例子,你都会吗?
Postgresql数据库character varying和character的区别说明
Intelligent software analysis platform embold
Jenkins 用户权限管理
[paper reading] maps: Multi-Agent Reinforcement Learning Based Portfolio Management System
恶魔奶爸 指南帖——简易版
恢复持久卷上的备份数据
Validutil, "Rethinking the setting of semi supervised learning on graphs"
如何满足医疗设备对安全性和保密性的双重需求?
怎样用Google APIs和Google的应用系统进行集成(1)—-Google APIs简介
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
Lingyun going to sea | yidiantianxia & Huawei cloud: promoting the globalization of Chinese e-commerce enterprise brands
Cantata9.0 | new features
guava多线程,futurecallback线程调用不平均
Mysql子查询关键字的使用方式(exists)
Apifox interface integrated management new artifact
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System