机房重构之充值

       最近机房重构一直在路上,心态已经从刚开始的无从下手到现在的照着葫芦画瓢。无论是三层还是七层,基本的思路都是一样的。最先最先的是写实体层,然后先写接口层的方法,然后就是D层去实现接口层的方法,接着就是工厂,B层,接着或者是外观层,然后是U层。由于我刚刚开始是按照一个窗体一个类来写的,并不是按照数据库中的表来写的,所以越来越觉得外观层没有用,毕竟B层就一个或者是俩个方法,敲着敲着就不写外观层啦。

     我们在第一次VB版的机房时候已经知道了业务逻辑啦,知道了这些窗体先执行什么,后执行什么。拿充值窗体为例,我们知道三次用到数据库,首先是查(卡号是不是在student表里),然后是插入(插入到recharge表中),最后是更新(更新student表中的余额)。

窗体:

                                        机房重构之充值_第1张图片

代码:

    接下来我们先看接口层的代码:

Public Interface IRecharge
    Function InterRecharge(RechargeEntity As Entity.Rechargeinfo) As DataTable

End Interface

      然后D层去实现接口层的方法:在D层涉及到了数据库的内容,在这里,先查询,然后插入,最后是更新。同时这里还用到了sqlhelp里面的方法。


Imports Entity
Imports IDAL
Imports System.Data.SqlClient
Imports Sqlhelper.SqlHelper
Public Class SqlServerRechargeDAL : Implements IRecharge

    Public Function InterRecharge(ByVal RechargeEntity As Entity.Rechargeinfo) As DataTable Implements IRecharge.InterRecharge
        Dim sql As String '定义字符串变量sql 用于存放要执行的sql语句
        Dim table As DataTable '定义表变量table 用于存储执行的结果并返回
        Dim ReEntity As New Entity.Rechargeinfo
        Dim stuEntity As New Entity.Studentinfo
        Dim paras As SqlParameter() = {New SqlParameter("@cardno", RechargeEntity.cardno)}
        sql = "select * from student_info where cardno = @cardno "
        table = Sqlhelper.SqlHelper.ExecSelect(sql, CommandType.Text, paras)

        If table.Rows.Count = 0 Then
            MsgBox("没有此卡号或此卡号已经退出,请重新输入!")
            Return table

        Else

            RechargeEntity.studentno = table.Rows(0).Item("studentno")
            RechargeEntity.addmoney = RechargeEntity.addmoney
            RechargeEntity.retime = Format(Now, "hh:mm:ss")
            RechargeEntity.User_ID = RechargeEntity.User_ID
            stuEntity.cash = table.Rows(0).Item("cash") + RechargeEntity.addmoney
            stuEntity.cardno = table.Rows(0).Item("cardno")
            Dim sql1 As String '定义字符串变量sql 用于存放要执行的sql语句
            Dim num As Integer  '定义表变量table 用于存储执行的结果并返回

            Dim paras1 As SqlParameter() = {New SqlParameter("@cardno", RechargeEntity.cardno),
                                           New SqlParameter("@studentno", RechargeEntity.studentno),
                                           New SqlParameter("@addmoney", RechargeEntity.addmoney),
                                           New SqlParameter("@redate", RechargeEntity.retime),
                                           New SqlParameter("@User_ID", RechargeEntity.User_ID),
                                           New SqlParameter("@Status", "没有使用")}

            sql1 = "insert into recharge_info(studentno,cardno,addmoney,redate,User_ID,Status) values(@cardno,@studentno,@addmoney,@redate,@User_ID,@Status)"
            num = Sqlhelper.SqlHelper.ExecuteNoQuery(sql1, CommandType.Text, paras1)
            Dim result1 As Boolean
            If num > 0 Then
                result1 = True
                Dim sql2 As String
                Dim num1 As Integer
                Dim result2 As Boolean
                Dim paras2 As SqlParameter() = {New SqlParameter("@cash", stuEntity.cash),
                                              New SqlParameter("@cardno", stuEntity.cardno)}
                sql2 = "update student_info set cash=@cash where cardno=@cardno"
                num1 = Sqlhelper.SqlHelper.ExecuteNoQuery(sql2, CommandType.Text, paras2)
                If num1 > 0 Then
                    result2 = True
                Else
                    result1 = False
                End If
            Else
                result1 = False
            End If
            Return table
        End If


    End Function
End Class
  

   工厂层是用来更换数据库用的:

Imports System.Reflection
Imports System.Configuration
Imports IDAL
Public Class RechargeFactory
    Private Shared ReadOnly AssemlyName As String = "DAL"
    '定义程序集名称变量D层命名空间的名字
    Private Shared db As String = ConfigurationManager.AppSettings("DB")
    Private Property AssemblyName As String = "DAL"
    '表示读配置文件如果配置文件中是sqlserver 就访问sqlserver数据库

    ''' <summary>
    ''' 充值窗口的抽象工厂
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function checkCfactory() As IRecharge
        '创建用户表的工厂
        Dim ClassName As String = AssemblyName + "." + db + "RechargeDAL"
        ''AssemblyName是程序集的名称,db+“UserDAL”是DAL层中的sqlServerUserDAL 如果不用SQLserver数据库,那么我们
        '在建一个类 比如我们访问access数据库 那么类名称就叫accessUserDal,把配置文件
        '中Value值改为Access 这样就是扩展而不是修改
        Dim irecharge As IRecharge '实例化接口
        '理解Assembly.Load(AssemblyName).CreateInstance(ClassName)
        irecharge = CType(Assembly.Load(AssemblyName).CreateInstance(ClassName), IRecharge)
        '将实例化的D层通过向上转型转换成接口类,然后通过调用接口类中的函数来调用D层中实现该接口的函数
        Return irecharge
    End Function

End Class

     接下来就是B层啦:

Imports IDAL
Imports Entity
Imports Factory
Public Class RechargeBLL
    Public Function Recheckcard(ByVal RechargeEntity As Entity.Rechargeinfo) As DataTable
        Dim ICheckCardno As IDAL.IRecharge
        Dim CheckFactory As New Factory.RechargeFactory

        ICheckCardno = CheckFactory.checkCfactory
        Dim ReBLLEntity As DataTable
        ReBLLEntity = ICheckCardno.InterRecharge(RechargeEntity)

        Return ReBLLEntity
    End Function

End Class

    外观:

Imports BLL
Public Class ReFacade

    Public Function cardnoFacade(ByVal RechargeEntity As Entity.Rechargeinfo) As DataTable

        Dim reBLL As New BLL.RechargeBLL
        Dim reEntity As DataTable
        reEntity = reBLL.Recheckcard(RechargeEntity)
        Return reEntity
    End Function
End Class

       U层:

Imports Facade
Imports Entity

Public Class frmrechargeUI

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim RechargeEntity As New Entity.Rechargeinfo
        RechargeEntity.cardno = txtcardno.Text.Trim
        RechargeEntity.addmoney = txtrecord.Text.Trim
        RechargeEntity.User_ID = frmLoginUI.txtusername.Text

        If txtcardno.Text = "" Or IsNumeric(txtcardno.Text) = False Then
            MsgBox("请输入卡号,并且为数字!")
        End If
        If txtrecord.Text = "" Or IsNumeric(txtrecord.Text) = False Then
            MsgBox("请输入卡号,并且为数字!")
        End If

        Dim RechargeFacade As New Facade.ReFacade
        Dim Rresult As DataTable
        Rresult = RechargeFacade.cardnoFacade(RechargeEntity)

        If Rresult.Rows.Count = 0 Then
        'If IsNothing(Rresult) Then
            MsgBox("没有此卡号!")
        Else
            TextBox1.Text = "充值卡号为:" & RechargeEntity.cardno & vbCrLf & vbCrLf & "充值金额是:" & RechargeEntity.addmoney + vbCrLf & vbCrLf & "充值时间为:" & Now & vbCrLf & vbCrLf & "充值老师为:" & RechargeEntity.User_ID
            MsgBox("充值成功!")
            TextBox1.Text = ""
            txtcardno.Text = ""
            txtrecord.Text = ""
        End If

    End Sub

End Class

明细节 :

        在敲的过程中遇到了很多问题,比如在执行U层的 下面的这句,也就是我注销的这句,无论是student表里面有没有数据,因为是isnothing 也就是包含表里面的字段,即使他里面没有数据,但是也会有表头字段,所以总是执行到U层的时候出现了错误。


<pre name="code" class="vb">'If IsNothing(Rresult) Then

            
 
          实体层就神略啦,我们有了很清晰的思路,那就是在各层之间注意点语法,同时在敲这个窗体的时候,我对返回值有了更加深刻的理解。

          重构,实践起来!!!!!!

       

你可能感兴趣的:(机房重构之充值)