以SqlHelper为例论面向对象中封装的使用(续)
2024-08-24 20:32:42
上文以SqlHelper为例说明了面向对象中封装的好处,但是上文只是简单封装,考虑下面代码的情况:
public static Activate GetByCode(string code)
{
List<SqlParameter> paraList = new List<SqlParameter>();
paraList.Add(new SqlParameter("@activateCode", code));
using (SqlDataReader reader = SqlHelper.ExecuteReader(Configuration.ConnectionString, CommandType.StoredProcedure, "GetActivateByCode", paraList.ToArray()))
{
while (reader.Read())
{
Activate result = new Activate();
result.Id = Utility.GetDbValue(reader, "Id", -);
result.CollegeId = Utility.GetDbValue(reader, "CollegeId", -);
result.ActivateCode = Utility.GetDbValue(reader, "ActivateCode", "");
result.Activated = Utility.GetDbValue(reader, "Activated", false);
result.StudentId = Utility.GetDbValue(reader, "StudentId", "");
result.StudyCenter = Utility.GetDbValue(reader, "StudyCenter", "");
result.StudentContact = Utility.GetDbValue(reader, "StudentContact", "");
result.Major = Utility.GetDbValue(reader, "Major", "");
result.Grade = Utility.GetDbValue(reader, "Grade", "");
result.CreatedTime = Utility.GetDbValue(reader, "CreatedTime", new DateTime(,,));
result.Disabled = Utility.GetDbValue(reader, "Disable", false); return result;
}
} return null;
}
如果这个查询语句要返回很多值的情况下,我们还要对查询结构进行一一匹配,map到定义好的数据结构<Activate>。我们希望对这个SqlHelper进行更高层次的封装,使他的返回结果自动map,像下面这样:
public static ACTIVATE GetByStudentId(string studentId)
{
IDbConnection connection = null;
try
{
connection = DAOHelper.GetConnection();
connection.Open(); //Query方法不仅执行了查询,并把返回的数据自动map成了ACTIVATE类型的数据,
//即数据库内的Id对应ACTIVATE的Id,数据库内的CollegeId对应ACTIVATE的CollegeId
//这个方法支持泛型的返回结果匹配
var list = connection.Query<ACTIVATE>(@"GetActivateByCode",studentId,CommandType.StoredProcedure) if (list != null && list.Count > )
return list[]; }
catch (Exception ex)
{
Trace.Write(ex);
return null;
}
finally
{
connection.Close();
}
return null;
}
像上面代码这样,这是对ADO.NET更为抽象的封装,他操作数据库的方式更加简单便捷,并支持返回结果的自动map。(这种技术称之为ORM)
值得注意的是,虽然越抽象的封装越方便易用,但也越不灵活,想想看如果以上代码中ACTIVATE的字段变掉怎么应对?所以,凡事都有利弊的两面,到底需要多大程度的封装,还要看我们项目的需求。
最新文章
- 【Java并发编程实战】-----“J.U.C”:CountDownlatch
- HDU 4289:Control(最小割)
- ping广播地址会如何(转)
- mariadb:InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
- Numpy 用法小结
- Linux SNMP oid
- 为什么我们可以使用while(~scanf(";%d";))读到文件末尾
- 后一个div无法遮挡住前一个有img的div
- asp.net 的page 基类页面 做一些判断 可以定义一个基类页面 继承Page类 然后重写OnPreLoad事件
- Android Studio系列教程一--下载和安装
- requirejs自己的学习
- codeforces A. Rook, Bishop and King 解题报告
- Visual Studio从此走入非Windows程序猿家
- Cupid&#39;s Arrow---hdu1756(判断点与多边形的位置关系 模板)
- 测序深度和覆盖度(Sequencing depth and coverage)
- JDK 自带工具试用(一)
- IO-02
- Hadoop基准测试(转载)
- poj 3356 AGTC(线性dp)
- 数据备份--dump(此作者有许多有用的博客文章)
热门文章
- 【sqli-labs】 less28 GET- Error based -All you Union&;Select Belong to us -String -Single quote with parenthesis(GET型基于错误的去除了Union和Select的单引号带括号字符串型注入)
- Typeclassopedia 阅读笔记:导言与 Functor
- Java中RunTime.getRunTime().addShutdownHook用法
- 模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)
- jQuery默认select选择第一个元素
- scrapy爬虫--10分钟入门
- eas之日志文件夹
- 编码的由来,ASCII编码,和字节的形成
- Spring Cloud-hystrix使用例子(七)
- 轻量级本地数据库SQLite在WinRT的使用