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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值