存储过程

    由于自己的认识不够深刻、自我管理能力比较欠缺,在小小的英语学习的压力下将机房收费系统个人重构版搁置了很久。如今,合作开发已接近尾声,终于将曾经落下的知识一一拾起,在这里写一写机房收费系统中所用到的知识和自己的收获。

    这次合作开发我负责写D层,但按照图和文档写的时候发现要写的函数都是单功能的增删改查,都只涉及到一张表,所以没必要用存储过程,其实不是用不到,而是我们最初的设计没有将系统中各个功能进行整合联系,没有在战略上做到最优,而是靠重复写代码来完成,省了脑力费了体力。最后调试,我分得的任务是结账,这里涉及到卡表(注册)、充值表(充值)、退卡表(退卡)等多张表的信息,在调试的时候我发现每个查询和更新都是单独的一个函数,这三个加起来就是6个函数,UI层、Facade层、B层、IDALDAL层,一层一层要调用多少次??经过和组长商量我决定重构一下整个结账功能。

重构前不用存储过程的代码:

UI

''' <summary>
    ''' 点击选项卡,查询所有的注册/充值/退卡信息,并结账
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tabOperateLog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabOperateLog.Click

        '定义卡表
        Dim dtCard As New DataTable
        '定义充值数据表
        Dim dtRecharge As New DataTable
        '定义退卡数据表
        Dim dtReturnCard As New DataTable
        '定义数据集
        Dim ds As New DataSet
        '定义用户外观对象
        Dim fUser As New Facade.UserFacade

 '定义字符串变量,参数为用户名,调用外观层方法,返回结账需要的购卡、充值、退卡信息
        Dim strUserID As String
        strUserID = cboUserID.Text
        ds = fUser.SelectCheckOut(strUserID)
 End Sub

Facade

Public Function SelectCheckOut(ByVal strUserID As String) As DataSet
        '定义数据集
        Dim ds As New DataSet
        '定义B层充值对象
        Dim bllRecharge As New BLL.RechargeManagerBLL
        '定义B层退卡对象
        Dim bllReturnCard As New BLL.ReturnCardManagerBLL
        '定义B层卡表
        Dim bllCard As New BLL.CardManagerBLL
        '定义卡表
        Dim dtCard As New DataTable
        '定义充值数据表
        Dim dtRecharge As New DataTable
        '定义退卡数据表
        Dim dtReturnCard As New DataTable
        Try
            dtCard = bllCard.QueryOperatorRegisterCard(strUserID) '注册,包含余额
            dtReturnCard = bllReturnCard.ManagerQueryReturnCash(strUserID) '退卡
            dtRecharge = bllRecharge.ManagerQueryRecharge(strUserID) '充值 
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 Return ds
    End Function

B层(包括注册、充值、退卡)

''' <summary>
    ''' 查找购卡信息的方法,根据操作员ID,返回Datatable集
    ''' </summary>
    ''' <param name="strUserID">根据操作员的ID实体,查找购卡信息</param>
    Public Function QueryOperatorRegisterCard(ByVal strUserID As String) As DataTable
        '实例化工厂
        Dim facCard As New Factory.Factory
        '定义接口变量
        Dim ICard As ComputerInterface.ISQLCardInfoDAL
        '创建CreateCardFactory工厂
        ICard = facCard.CreateCardFactory()
        Return ICard.SelectManagerCard(strUserID)
    End Function

''' <summary>
	''' 以管理员的权限在结账的过程中根据操作员ID查询充值记录,返回datatable。
	''' </summary>
    ''' <param name="strUserID">管理员在结账中提供的操作员ID实体</param>
    Public Function ManagerQueryRecharge(ByVal strUserID As String) As DataTable
        '实例化Factory对象
        Dim facRecharge As New Factory.Factory
        '定义接口变量
        Dim IRecharge As ComputerInterface.ISQLRechargeInfoDAL
        '创建CreateRechargeFactory的工厂
        IRecharge = facRecharge.CreateRechargeFactory()
        '返回数据集 
        Return IRecharge.ManagerSelectRecharge(strUserID)
    End Function


''' <summary>
	''' 管理员查找退卡信息的方法,根据操作员ID实体查找,返回datatable
	''' </summary>
    ''' <param name="strUserID">管理员查找,根据操作员ID实体查找</param>
    Public Function ManagerQueryReturnCash(ByVal strUserID As String) As DataTable
        '实例化Factory对象
        Dim facReturnCard As New Factory.Factory
        '定义接口变量
        Dim IReturnCardInfo As ComputerInterface.ISQLReturnCardInfoDAL
        '创建CreateReturnCardFactory工厂
        IReturnCardInfo = facReturnCard.CreateReturnCardFactory()
        '返回数据集
        Return IReturnCardInfo.ManagerSelectReturnCard(strUserID)
    End Function

接口层:(因为太多,所以只列举注册,说明问题即可)

''' <summary>
    ''' 管理员读取购卡的接口,根据实体中操作员strUserID查找表T_CardInfo中购卡信息,显示的字段为CardID,balance,registerDate,registerTime
	''' </summary>
    ''' <param name="strUserID"></param>

    Function SelectManagerCard(ByVal strUserID As String) As DataTable

D层:(只含注册)

 ''' <summary>
    ''' 管理员读取购卡的接口,根据实体中操作员strUserID查找表T_CardInfo中购卡信息,显示的字段为CardID,balance,registerDate,registerTime
    ''' </summary>
    ''' <param name="strUserID">操作员用户名</param>
    Public Function SelectOperatorCard(ByVal strUserID As String) As DataTable Implements ISQLCardInfoDAL.SelectManagerCard
        Dim strSql As String
        Dim dt As DataTable
        Dim sqlParames As SqlParameter()
        sqlParames = New SqlParameter() {
            New SqlParameter("@userID", strUserID)
            }
        strSql = "select * from T_CardInfo where userID=@userID and isCheck='未结账'"
        dt = sqlHelper.GetDataTable(sqlParames, CommandType.Text, strSql)
        Return dt
    End Function


重构后使用存储过程的代码:

UI层

''' <summary>
    ''' 点击选项卡,查询所有的注册/充值/退卡信息,并结账
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tabOperateLog_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tabOperateLog.Click
        '定义数据集
        Dim ds As New DataSet
        '定义用户外观对象
        Dim fUser As New Facade.UserFacade
 '定义字符串变量,参数为用户名,调用外观层方法,返回结账需要的注册、充值、退卡信息
        Dim strUserID As String
        strUserID = cboUserID.Text
        ds = fUser.SelectCheckOut(strUserID)
        End Sub

Facade

''' <summary>
    ''' 根据操作员用户名,查询注册、充值、退卡信息,返回数据集
    ''' </summary>
    ''' <param name="strUserID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectCheckOut(ByVal strUserID As String) As DataSet
        '定义数据集
        Dim ds As New DataSet
        '定义B层卡表
        Dim bllCard As New BLL.CardManagerBLL
        ds = bllCard.SelectCheckOut(strUserID)
        Return ds
    End Function

B

''' 根据操作员用户名,查询注册、充值、退卡信息,返回数据集
    ''' </summary>
    ''' <param name="strUserID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SelectCheckOut(ByVal strUserID As String) As DataSet
        '实例化工厂
        Dim facCard As New Factory.Factory
        '定义接口变量
        Dim ICard As ComputerInterface.ISQLCardInfoDAL
        '创建CreateCardFactory工厂
        ICard = facCard.CreateCardFactory()
        Return ICard.SelectCheckOut(strUserID)
    End Function

接口层

''' <summary>
    ''' 根据操作员用户名查询注册、充值、退卡信息,返回数据集
    ''' </summary>
    ''' <param name="strUserID">操作员用户名</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function SelectCheckOut(ByVal strUserID As String) As DataSet

D

Function SelectCheckOut(ByVal strUserID As String) As DataSet Implements ISQLCardInfoDAL.SelectCheckOut
        Dim strSql As String
        Dim ds As New DataSet
        Dim sqlParames As SqlParameter()
        sqlParames = New SqlParameter() {
           New SqlParameter("@userID", strUserID)
       }
        strSql = "PROC_SelectCheckOut"
        ds = sqlHelper.GetDataset(sqlParames, CommandType.StoredProcedure, strSql)
        Return ds
    End Function

存储过程

-- =============================================
-- Author:		谷海燕
-- Create date: 2013-12-6 
-- Description:	根据操作员用户名查询注册、充值、退卡信息
-- =============================================
CREATE PROCEDURE PROC_SelectCheckOut 
	@userID varchar(20)
AS
BEGIN
	select * from T_CardInfo where userID =@userID and isCheck ='未结账'
	select * from T_RechargeInfo where userID=@userID and isCheck ='未结账'
	select * from T_ReturnCardInfo  where userID=@userID and isCheck ='未结账'
END
GO

总结

记得一位同学评论博客时说过一句话:不断地抽象和封装。的确,代码的重构就是一个不断地抽象和封装的过程,我们必须时刻注意“合并同类项”,减少重复代码即增加代码的复用性,这样才是灵活的。记得刚开始做系统的时候一点都不愿意改动,因为不懂或者不熟悉,害怕改一点就一发不可收拾,现在呢,哪里不好改哪里,懂了就不会怕错不会闲麻烦了。

原来一直以为写代码是脑力活,现在越发觉得就是体力活了。


你可能感兴趣的:(存储过程)