上文以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的字段变掉怎么应对?所以,凡事都有利弊的两面,到底需要多大程度的封装,还要看我们项目的需求。

最新文章

  1. 【Java并发编程实战】-----“J.U.C”:CountDownlatch
  2. HDU 4289:Control(最小割)
  3. ping广播地址会如何(转)
  4. mariadb:InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes
  5. Numpy 用法小结
  6. Linux SNMP oid
  7. 为什么我们可以使用while(~scanf(&quot;%d&quot;))读到文件末尾
  8. 后一个div无法遮挡住前一个有img的div
  9. asp.net 的page 基类页面 做一些判断 可以定义一个基类页面 继承Page类 然后重写OnPreLoad事件
  10. Android Studio系列教程一--下载和安装
  11. requirejs自己的学习
  12. codeforces A. Rook, Bishop and King 解题报告
  13. Visual Studio从此走入非Windows程序猿家
  14. Cupid&#39;s Arrow---hdu1756(判断点与多边形的位置关系 模板)
  15. 测序深度和覆盖度(Sequencing depth and coverage)
  16. JDK 自带工具试用(一)
  17. IO-02
  18. Hadoop基准测试(转载)
  19. poj 3356 AGTC(线性dp)
  20. 数据备份--dump(此作者有许多有用的博客文章)

热门文章

  1. 【sqli-labs】 less28 GET- Error based -All you Union&amp;Select Belong to us -String -Single quote with parenthesis(GET型基于错误的去除了Union和Select的单引号带括号字符串型注入)
  2. Typeclassopedia 阅读笔记:导言与 Functor
  3. Java中RunTime.getRunTime().addShutdownHook用法
  4. 模拟人的手指在UI上滑动时3D模型跟随着移动(Unity)
  5. jQuery默认select选择第一个元素
  6. scrapy爬虫--10分钟入门
  7. eas之日志文件夹
  8. 编码的由来,ASCII编码,和字节的形成
  9. Spring Cloud-hystrix使用例子(七)
  10. 轻量级本地数据库SQLite在WinRT的使用