1. 程式人生 > >【機房重構】註冊

【機房重構】註冊

    機房重構開始有一斷時間了,這個過程對於我來說並不容易,由開始的三層登入實現,到後來的七層登入實現,很多人說只要這條線通了以後就容易多了,但是通過這個過程對於我來說並沒有那麼容易,這個過程總會出現這樣那樣的問題。

    以註冊為例,我的 資料庫設計的是卡表和學生表分開的,當初為了敲出另外一條線,沒有首先判斷卡號是否存在,只是單純的將資訊插入了card表,沒有插入student表,因為還不知道怎麼同時插入兩張表,也不知道怎麼怎麼要判斷完卡號之後再去決定要不要將相關資訊插入資料庫。通過與同學們的交流和我的摸索,寫出瞭如下程式碼,為了方便我在D層傳了兩個實體,實現同時插入Card表和Student表。程式碼如下:

    第一步,封裝實體,將登錄檔中的各個欄位都寫入實體中,方便以後各層的呼叫,這裡程式碼比較簡單就省略了。

    第二步,建立介面方法

<span style="font-family:KaiTi_GB2312;font-size:18px;"> ''' <summary>
    ''' 建立註冊的方法,為了方便在此兩個引數
    ''' </summary>
    ''' <param name="card"></param>
    ''' <param name="student"></param>
    ''' <returns></returns>
    ''' <remarks>2015年4月10日11:21:38</remarks>
    Function RegisterCard(ByVal card As Entity.EntityCard, ByVal student As Entity.EntityStudent) As Boolean</span>

    第三步,建立抽象工廠+反射

<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports IDAL
Imports System.Data
Imports System.Reflection
Imports System.Configuration
'Factory
Public Class FactoryDB
    Private Shared ReadOnly AssemblyName As String = "DAL" '資料程式集名稱
    Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DB")
    Dim strCard As String = System.Configuration.ConfigurationSettings.AppSettings("DB")
  
    ''' <summary>
    ''' 例項化一個DAL中的Card表的類
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>2015-4-10 11:32:48</remarks>
    Public Function CreateCard() As IDAL.ICard

        Dim classname As String = "DAL" + "." + strDB + "CardDAL"

        Return CType(Assembly.Load(AssemblyName).CreateInstance(classname), IDAL.ICard)
        '' Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & strCard), IDAL.ICard)
    End Function</span>

     第四步,D層實現介面的方法,這裡會用到sqlHelper,對於封裝的sqlHelper詳見部落格:【機房重構】sqlHelper.

<span style="font-family:KaiTi_GB2312;font-size:18px;">    ''' <summary>
    ''' 註冊新卡
    ''' </summary>
    ''' <param name="card"></param>
    ''' <param name="student"></param>
    ''' <returns>返回Boolean型</returns>
    ''' <remarks>2015-4-10 14:19:00</remarks>
    Public Function RegisterCard(card As EntityCard, student As EntityStudent) As Boolean Implements ICard.RegisterCard
        Dim Sql As String
        Dim table As Boolean
        Dim sqlParams As SqlParameter() = {New SqlParameter("@CardNo", card.Card),
                                           New SqlParameter("@StudentNo", card.Student),
                                           New SqlParameter("@Date", card.RDate),
                                           New SqlParameter("@Time", card.RTime),
                                           New SqlParameter("@Balance", card.BL),
                                           New SqlParameter("@ID", card.ID),
                                           New SqlParameter("@Check", card.Check),
                                           New SqlParameter("@Type", card.CardType),
                                           New SqlParameter("@Status", card.CardStatus)}
        Sql = "Insert into Card_Info(CardNo,StudentNo,RegisterDate,RegisterTime,Balance,IsCheck,userID,Type,Status) Values(@CardNo,@StudentNo,@Date,@Time,@Balance,@Check,@ID,@Type,@Status)"
        table = sqlHelper.ExecAddDelUpdate(Sql, CommandType.Text, sqlParams)
    
        If table = True Then


            Dim Asql As String
            Dim flag As Boolean
            Dim params As SqlParameter() = {New SqlParameter("@StudentNoo", student.Student),
                                            New SqlParameter("@Name", student.StudentName),
                                            New SqlParameter("@Sex", student.StudentSex),
                                            New SqlParameter("@Department", student.Dept),
                                            New SqlParameter("@Grade", student.StudentGrade),
                                            New SqlParameter("@Class", student.StudentClass),
                                            New SqlParameter("@Comment", student.SCommant)}
            Asql = "Insert into Student_Info(StudentNo,Name,Sex,Department,Grade,Class,Comment) Values(@StudentNoo,@Name,@Sex,@Department,@Grade,@Class,@Comment)"
            flag = sqlHelper.ExecAddDelUpdate(Asql, CommandType.Text, params)
            Return flag
        End If
        Return table
    End Function</span>

    第五步,B層實現抽象工廠和介面的方法,在這裡要進行邏輯判斷。

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class CardBLL
    '實現抽象工廠和介面的方法
    ''' <summary>
    ''' 註冊卡號
    ''' </summary>
    ''' <param name="card"></param>
    ''' <returns></returns>
    ''' <remarks>2015-4-10 14:57:22</remarks>
    Public Function AddCard(ByVal card As Entity.EntityCard, ByVal student As Entity.EntityStudent) As Boolean

        Dim factory As New Factory.FactoryDB
        Dim Acard As IDAL.ICard
        Dim Aflag As DataTable
        Dim flag As Boolean
        Acard = factory.CreateCard
        Aflag = Acard.IsExitCard(card)

        If Aflag.Rows.Count = 0 Then
            flag = Acard.RegisterCard(card, student)
            If flag = True Then
                MsgBox("註冊卡號成功", , "提示")
            Else
                MsgBox("未註冊成功", , "提示")
            End If
        Else

            MsgBox("此卡號已存在", , "提示")
        End If

        Return flag
    End Function
End Class</span>

    第六步,Facade層,呼叫B層方法將U層資料傳入B層或返回資料給U層

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class RegisterCardFAC
    ''' <summary>
    ''' 註冊卡號
    ''' </summary>
    ''' <param name="card"></param>
    ''' <returns></returns>
    ''' <remarks>2015-4-10 15:04:41</remarks>
    Public Function AddCard(ByVal card As Entity.EntityCard, ByVal student As Entity.EntityStudent) As Boolean
        Dim add As New BLL.CardBLL()
        Dim flag As Boolean
        flag = add.AddCard(card, student)
        Return flag
    End Function
End Class
</span>

    第七步,U層,將輸入的內容傳給實體,通過外觀層,進行傳參。

<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class frmRegister


    Private Sub frmRegister_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboType.Items.Add("臨時使用者")
        ComboType.Items.Add("固定使用者")
        comboSex.Items.Add("男")
        comboSex.Items.Add("女")
        ComboStatus.Items.Add("使用")
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        Dim CardFacade As New Facade.RegisterCardFAC
        '例項化實體類卡
        Dim card As New Entity.EntityCard
        '例項化實體類學生
        Dim student As New Entity.EntityStudent
        '呼叫卡

        card.ID = LoginUI.txtUserName.Text.Trim()
        card.BL = txtChargeMoney.Text
        card.Student = txtStudentNo.Text
        card.Card = txtCardNo.Text.Trim()
        card.CardType = ComboType.Text
        card.RDate = DateTime.Now.ToLongDateString()
        card.RTime = DateTime.Now.ToLongTimeString()
        card.CardStatus = ComboStatus.Text
        card.Check = "未結賬"

        student.Student = txtStudentNo.Text
        student.StudentName = txtStudentName.Text
        student.StudentSex = comboSex.Text
        student.Dept = txtDept.Text
        student.StudentGrade = txtGrade.Text
        student.StudentClass = txtClass.Text
        student.SCommant = txtComment.Text

        Dim flag1 As Boolean
        flag1 = CardFacade.AddCard(card, student)

    End Sub

End Class</span>

    總結:

    每一個功能都是按照這樣的一個順序進行編寫的,當敲的多了發現其實並不難,機房重構和第一次機房的時候是一樣的,萬事開頭難,當明白之後發現敲程式碼是一個很享受的過程,不過敲完之後的除錯更是一個技術活。註冊這個功能涉及到兩個實體,兩個表,這裡可以定義一個超體,將卡表和學生表合在一起寫作一個實體類,這樣就不用傳兩個實體了,聽同學們說也可以使用儲存過程,這個我還沒有嘗試,在接下來時間裡慢慢的去熟悉它。機房,我會加油的!