一个优秀的程序员就是要尽可能减少自己的代码量。我们日常的工作或者学习中都有可能遇到多个窗体结构类似,或者大同小异的情况,这时候如果在每个窗体都写一遍代码,或者纯粹的复制粘贴就太水了。想要偷懒的时候正是我们发现和创造的机会,我们的祖先也是这么过来的。
我在做机房收费系统个人重构版的时候,就遇到了基本数据维护、查看上机状态、上机信息统计和操作员工作记录四个窗体都组要用到组合查询而且窗体结构一致。
四个窗体中除了字段名不一致,其他的内容都一样,这种情况下我们用窗体的继承最合适了。在父窗体中写好公共的部分,不一致的地方可以写一个虚方法,然后让子类重写父类的虚方法。这样我们只需要一个U层,一个B层,一个接口,一个抽象工厂,一个D层然后子类重写虚方法就可以实现四个窗体各自的功能了。
U层主要判断一下控件是否为空,给实体参数和B层方法传值传值,以及定义转换数据库字段的虚方法和获取表名的虚方法,这些虚方法在不同的子类中被重写以实现不同功能。
'************************************************* '作者:邵鸿鑫 '小组: '说明:组合查询模板 '创建日期:2014.7.9 '版本号: '**********************************************/ Public Class frmGroupQuery Public thisgroupquery As New Entity.GroupQueryEnpty '定义一个实体参数 Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles btnQuery.Click '第一行组合关系为空 If cmbRelation1.Text = "" Then '判断第一行控件内容是否为空 If cmbField1.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation1.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent1.Text = "" Then MsgBox("请输入要查询的内容") End If End If ' 当第一个组合关系不为空时 If cmbRelation1.Text <> "" Then If cmbField1.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation1.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent1.Text = "" Then MsgBox("请输入要查询的内容") ElseIf cmbField2.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation2.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent2.Text = "" Then MsgBox("请输入要查询的内容") End If End If ' 当第二个组合关系不为空时 If cmbRelation2.Text <> "" Then If cmbField1.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation1.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent1.Text = "" Then MsgBox("请输入要查询的内容") ElseIf cmbField2.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation2.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent2.Text = "" Then MsgBox("请输入要查询的内容") ElseIf cmbField3.Text = "" Then MsgBox("请输入字段名") ElseIf cmbOperation3.Text = "" Then MsgBox("请输入操作符") ElseIf txtContent3.Text = "" Then MsgBox("请输入要查询的内容") End If End If '给实体层传参 Dim thisgroupquery As New Entity.GroupQueryEnpty thisgroupquery.cmbField1_text = GetDBName(cmbField1.Text.Trim()) thisgroupquery.cmbField2_text = GetDBName(cmbField2.Text.Trim()) thisgroupquery.cmbField3_text = GetDBName(cmbField3.Text.Trim()) thisgroupquery.cmbOperation1_text = cmbOperation1.Text.Trim() thisgroupquery.cmbOperation2_text = cmbOperation2.Text.Trim() thisgroupquery.cmbOperation3_text = cmbOperation3.Text.Trim() thisgroupquery.txtContent1_text = txtContent1.Text.Trim() thisgroupquery.txtContent2_text = txtContent2.Text.Trim() thisgroupquery.txtContent3_text = txtContent3.Text.Trim() thisgroupquery.cmbRelation1_text = GetDBName(cmbRelation1.Text.Trim()) thisgroupquery.cmbRelation2_text = GetDBName(cmbRelation2.Text.Trim()) thisgroupquery.GetTable = GetTable() '给B层GroupQuery方法传递参数 Dim db As New BLL.GroupQueryBLL If db.GroupQuery(thisgroupquery) Is Nothing Then MsgBox("没有记录,请重新设置查询条件", vbOKOnly, vbExclamation) DataGridView1.DataSource = Nothing Else DataGridView1.DataSource = db.GroupQuery(thisgroupquery) End If End Sub ' 定义虚函数GetDBName,获取不同数据库的字段名 Protected Overridable Function GetDBName(ByVal control As String) As String Return "" End Function ' 定义虚函数GetDBName,获取不同数据库的表名 Protected Overridable Function GetTable() As String Return "" End Function Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles btnExit.Click Me.Hide() End Sub '组合关系一不为空后,显示第二行查询条件 Private Sub cmbRelation1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbRelation1.SelectedIndexChanged cmbField2.Enabled = True cmbOperation2.Enabled = True cmbRelation2.Enabled = True txtContent2.Enabled = True End Sub '组合关系二不为空后,显示第三行查询条件 Private Sub cmbRelation2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbRelation2.SelectedIndexChanged cmbField3.Enabled = True cmbOperation3.Enabled = True txtContent3.Enabled = True End Sub End Class
B层写法跟以前分层写法一样,引用工厂和接口返回查询到的表。
<span style="font-family:KaiTi_GB2312;font-size:18px;">'************************************************* '作者:邵鸿鑫 '小组: '说明:组合查询B层 '创建日期:2014.7.17 '版本号: '**********************************************/ Imports IDAL Imports Entity Public Class GroupQueryBLL Public Function GroupQuery(ByVal queries As Entity.GroupQueryEnpty) As DataTable Dim factory As New Factory.GroupQueryFactory Dim igroupquery As IGroupQuery = factory.GroupQuery Dim table = igroupquery.GroupQuery(queries) '检查D层返回的数据表中是否有数据 If table.Rows.Count = 0 Then Return Nothing Else Return table End If End Function End Class</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">'************************************************* '作者:邵鸿鑫 '小组: '说明:组合查询D层 '创建日期:2014.7.17 '版本号: '**********************************************/ Imports System.Data.SqlClient Imports IDAL Public Class SqlserverGroupQueryDAL : Implements IGroupQuery Function GroupQuery(ByVal queries As Entity.GroupQueryEnpty) As DataTable Implements IGroupQuery.GroupQuery Dim strSQL As String = "PROC_GroupQuery" '调用存储过程 Dim prams As SqlParameter() = {New SqlParameter("@cmbField1", queries.cmbField1_text), New SqlParameter("@cmbField2", queries.cmbField2_text), New SqlParameter("@cmbField3", queries.cmbField3_text), New SqlParameter("@cmbOperation1", queries.cmbOperation1_text), New SqlParameter("@cmbOperation2", queries.cmbOperation2_text), New SqlParameter("@cmbOperation3", queries.cmbOperation3_text), New SqlParameter("@txtContent1", queries.txtContent1_text), New SqlParameter("@txtContent2", queries.txtContent2_text), New SqlParameter("@txtContent3", queries.txtContent3_text), New SqlParameter("@cmbRelation1", queries.cmbRelation1_text), New SqlParameter("@cmbRelation2", queries.cmbRelation2_text), New SqlParameter("@tableName", queries.GetTable)} '设置参数 Dim helper As New SqlHelper Dim table As New DataTable table = helper.GetDataTable(strSQL, CommandType.StoredProcedure, prams) Return table End Function End Class</span>
存储过程
<span style="font-family:KaiTi_GB2312;font-size:18px;">-- ============================================= -- Author: 邵鸿鑫 -- Create date: 20140719 -- Description: 组合查询 -- ============================================= ALTER PROCEDURE [dbo].[PROC_GroupQuery] -- Add the parameters for the stored procedure here @cmbField1 varchar(10), @cmbOperation1 varchar(10), @txtContent1 varchar(10), @cmbField2 varchar(10), @cmbOperation2 varchar(10), @txtContent2 varchar(10), @cmbField3 varchar(10), @cmbOperation3 varchar(10), @txtContent3 varchar(10), @cmbRelation1 varchar(10), @cmbRelation2 varchar(10), @tableName varchar(20) AS declare @TempSql varchar(500)--临时存放sql语句 BEGIN SET @TempSql='SELECT * FROM '+@tableName +' WHERE ' +@cmbField1 +@cmbOperation1+char(39) + @txtContent1 + char(39) if @cmbRelation1 != '' BEGIN SET @TempSql=@TempSql+@cmbRelation1+CHAR(32)+@cmbField2 +@cmbOperation2+CHAR(39)+@txtContent2+CHAR(39) if @cmbRelation2!= '' BEGIN SET @TempSql=@TempSql+@cmbRelation2+CHAR(32)+@cmbField3+@cmbOperation3+CHAR(39)+@txtContent3+CHAR(39) END END EXECUTE(@TempSql) END</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;">'************************************************* '作者:邵鸿鑫 '小组: '说明:组合查询抽象工厂 '创建日期:2014.7.17 '版本号: '**********************************************/ Imports System.Reflection Imports System.Configuration Imports IDAL Public Class GroupQueryFactory Private Shared ReadOnly AssemblyName As String = "DAL" '声明程序集名称 Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB") '读取配置文件到db Public Function GroupQuery() As IGroupQuery Dim className As String = AssemblyName + "." + db + "GroupQueryDAL" Dim igroupquery As IGroupQuery igroupquery = CType(Assembly.Load(AssemblyName).CreateInstance(className), IGroupQuery) Return igroupquery End Function End Class </span>
然后选择被继承的窗体
之后就是重写父类的虚方法
<span style="font-family:KaiTi_GB2312;font-size:18px;">'************************************************* '作者:邵鸿鑫 '小组: '说明:查询操作员工作记录U层 '创建日期:2014.7.18 '版本号: '**********************************************/ Public Class frmworklog '重写转换成数据库字段方法 Protected Overrides Function GetDBName(ByVal control As String) As String Select Case (control) Case "教师" Return "userName" Case "上班日期" Return "loginTime" Case "上班时间" Return "loginTime" Case "下班日期" Return "offTime" Case "下班时间" Return "offTime" Case "机器号" Return "computer" Case "与" Return "and" Case "或" Return "or" Case Else Return "" End Select End Function '重写获得表名方法 Protected Overrides Function GetTable() As String thisgroupquery.GetTable = "Work_info" Return thisgroupquery.GetTable End Function Public Sub frmworklog_load(sender As Object, e As EventArgs) Handles MyBase.Load '给字段赋初值 cmbField1.Items.Add("教师") cmbField1.Items.Add("上班日期") cmbField1.Items.Add("上班时间") cmbField1.Items.Add("下班日期") cmbField1.Items.Add("下班时间") cmbField1.Items.Add("机器号") cmbField2.Items.Add("教师") cmbField2.Items.Add("上班日期") cmbField2.Items.Add("上班时间") cmbField2.Items.Add("下班日期") cmbField2.Items.Add("下班时间") cmbField2.Items.Add("机器号") cmbField3.Items.Add("教师") cmbField3.Items.Add("上班日期") cmbField3.Items.Add("上班时间") cmbField3.Items.Add("下班日期") cmbField3.Items.Add("下班时间") cmbField3.Items.Add("机器号") End Sub End Class</span>为了避免用户不按顺序组合查询条件,把第二行和第三行查询条件以及第二个组合关系设的属性置为不可用,只有第一个组合关系不为空时第二行查询条件和第二个组合关系才能使用,同理第二个组合关系不为空时第三行组合条件才能使用。
言归正传这一切的一切都是为了减少我们的工作量,人类的进步其实就是一次次为了“偷懒”而不断探索的过程。文章中有不足之处还请大神们批评指正,希望能对大家有所帮助。