C#教程之自己动手写映射第四节[封装删除]

一、动机

  我们在借助于SqlHelper删除数据的时候,一般的代码如下:

 1 /*
 2  *
 3  * 创建人:李林峰
 4  *
 5  * 时  间:2012-07-26
 6  *
 7  * 描  述:借助于SqlHelper删除表的数据
 8  *
 9  */
10 
11 using System.Data;
12 using CSharp.Core;
13 
14 namespace CSharp.Remove
15 {
16     class Common
17     {
18         public void RemoveInvoke()
19         {
20             string conn = "Data Source=192.168.1.8;Initial Catalog=Test;uid=sa;pwd=123!@#abc;";
21 
22             //根据编号删除表A数据
23             string strSQLA = string.Format("DELETE FROM A WHERE ID = {0}", 1);
24             SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQLA);
25 
26             //根据编号删除表B数据
27             string strSQLB = string.Format("DELETE FROM B WHERE ID = {0}", 1);
28             SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQLB);
29 
30             //根据编号删除表C数据
31             string strSQLC = string.Format("DELETE FROM C WHERE ID = {0}", 1);
32             SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQLC);
33 
34             //根据编号删除表D数据
35             string strSQLD = string.Format("DELETE FROM D WHERE ID = {0}", 1);
36             SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQLD);
37 
38             //根据编号删除表E、F、G、H.........数据
39         }
40     }
41 }

     如上所示:我们在进行表数据根据编号删除的时候,重复不停的写着:

1 //根据编号删除表A数据
2 string strSQLA = string.Format("DELETE FROM A WHERE ID = {0}", 1);
3 SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strSQLA);

  在我们反复书写的同时,有没能停下脚步思考下这些代码是否有重复的地方或是可以重用的地方呢?经过思考我们可以看出变化的部分只是SQL语句不同:

1 DELETE FROM A WHERE ID = {0}

  我们再进一步思考SQL中变化的部分与不变的部分:

1 DELETE FROM 变化的表名称 WHERE 变化的主键 = 变化的主键编号

  到了这里,我们基本分离出了所有删除语句中变化的部分和变化的部分。

二、实现

  通过上面的动机,我们可以在SqlHelper的基础上二次封装如下方法,以简化我们开发过程中的工作:

 1         /// <summary>
 2         /// 封装后
 3         /// </summary>
 4         public void RemoveInvokeByHelper()
 5         {
 6             //根据编号删除表A数据
 7             RemoveHelper.Main(conn, "A", "ID", "1");
 8             //根据编号删除表B数据
 9             RemoveHelper.Main(conn, "B", "ID", "1");
10             //根据编号删除表C数据
11             RemoveHelper.Main(conn, "C", "ID", "1");
12             //根据编号删除表D数据
13             RemoveHelper.Main(conn, "D", "ID", "1");
14             //根据编号删除表E、F、G、H.........数据
15         }

  简单应用代码如下:

  1 /*
  2  *
  3  * 创建人:李林峰
  4  *
  5  * 时  间:2012-07-26
  6  *
  7  * 描  述:借助于SqlHelper删除表的数据
  8  *
  9  */
 10 
 11 using System;
 12 using System.Collections.Generic;
 13 using System.Data;
 14 using System.Text;
 15 
 16 namespace CSharp.Core
 17 {
 18     public class RemoveHelper
 19     {
 20         public static void Main(string conn, string tableName, string primaryKey, string keyValue)
 21         {
 22             string strRemoveSQL = string.Format("DELETE FROM {0} WHERE {1} = {2}", tableName, primaryKey, keyValue);
 23             SqlHelper.ExecuteNonQuery(conn, CommandType.Text, strRemoveSQL);
 24         }
 25     }
 26 }
 27 
 28 
 29 /*
 30  *
 31  * 创建人:李林峰
 32  *
 33  * 时  间:2012-07-26
 34  *
 35  * 描  述:常量类
 36  *
 37  */
 38 
 39 using System.Configuration;
 40 
 41 namespace CSharp.DLL
 42 {
 43     /// <summary>
 44     /// 常量类
 45     /// </summary>
 46     public class Constant
 47     {
 48         /// <summary>
 49         /// 连接字符串
 50         /// </summary>
 51         public static string CONNSTRING = ConfigurationManager.ConnectionStrings["Conn"].ToString();
 52     }
 53 }
 54 
 55 
 56 
 57 
 58 /*
 59  *
 60  * 创建人:李林峰
 61  *
 62  * 时  间:2012-07-26
 63  *
 64  * 描  述:删除员工实例
 65  *
 66  */
 67 
 68 using System;
 69 using CSharp.Core;
 70 
 71 namespace CSharp.DLL
 72 {
 73     public class Employee
 74     {
 75         public static readonly string tableName = "Employee";
 76         public static readonly string primaryKey = "EmployeeID";
 77         /// <summary>
 78         /// 删除员工
 79         /// </summary>
 80         /// <param name="keyValue"></param>
 81         public static void Remove(string keyValue)
 82         {
 83             RemoveHelper.Main(Constant.CONNSTRING, tableName, primaryKey, keyValue);
 84         }
 85     }
 86 }
 87 
 88 
 89 
 90 /*
 91  *
 92  * 创建人:李林峰
 93  *
 94  * 时  间:2012-07-26
 95  *
 96  * 描  述:删除消息实例
 97  *
 98  */
 99 
100 using System;
101 using CSharp.Core;
102 
103 namespace CSharp.DLL
104 {
105     class Message
106     {
107         public static readonly string tableName = "Message";
108         public static readonly string primaryKey = "MessageID";
109         /// <summary>
110         /// 删除消息
111         /// </summary>
112         /// <param name="keyValue"></param>
113         public static void Remove(string keyValue)
114         {
115             RemoveHelper.Main(Constant.CONNSTRING, tableName, primaryKey, keyValue);
116         }
117     }
118 }

  项目结构如下:

三、总结

  通过上面的例子一步一步对我们日常的重复代码进行了封装,直到我们认为变化的地方是一个不可分割的点,删除时我们暂时并没有用到“映射”的概念,大家可以继续想一下,是不是我们的“tableName”,“MessageID”等可以统计归类到.xml文件中,我们的底层去依赖个xml,使程序的结构变的更清晰,后面的章节我们会继续介绍。

四、版权

  转载请注明出处:http://www.cnblogs.com/iamlilinfeng

 

转载于:https://www.cnblogs.com/iamlilinfeng/archive/2012/07/26/2610558.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值