设为首页 加入收藏

TOP

以SqlHelper为例论面向对象中封装的使用(续)
2019-09-03 03:21:58 】 浏览:14
Tags:SqlHelper 面向 对象 封装 使用

   

  上文以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", -1); result.CollegeId = Utility.GetDbValue(reader, "CollegeId", -1); 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(1900,1,1)); 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 > 0) return list[0]; } catch (Exception ex) { Trace.Write(ex); return null; } finally { connection.Close(); } return null; }

  像上面代码这样,这是对ADO.NET更为抽象的封装,他操作数据库的方式更加简单便捷,并支持返回结果的自动map。(这种技术称之为ORM

  值得注意的是,虽然越抽象的封装越方便易用,但也越不灵活,想想看如果以上代码中ACTIVATE的字段变掉怎么应对?所以,凡事都有利弊的两面,到底需要多大程度的封装,还要看我们项目的需求。

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇OrcharNoCMS中的发布订阅使用 下一篇[设计模式]解释器(Interpreter)之..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目