重构版机房收费系统构建思路

时间:2022-12-20 15:39:42

重构机房收费系统已经一周了快,从最开始的毫无头绪,到现在的思路明晰,真是经过了各种煎熬啊。以下是我的包图:

重构版机房收费系统构建思路

这里用一个接口来解耦了B层和D层。这样B层和D层互相就没有了直接联系。工厂用反射和配置文件来访问数据库,这样换数据库也不用改D层代码,只要把字符串“SQLserver”改成其他数据库的名字如:“access”。就可以访问access数据库了。

以一个注册新学生为例:

ChargeFactory中的代码:

'生产一个istudent接口。用到了反射和配置文件,这里U层的APP.config已经配置好。
Imports System.Reflection'引入反射Imports System.ConfigurationImports [Interface]Public Class studentFactory    Private Shared ReadOnly AssemblyName As String = "ChargeDAL"    Private Shared ReadOnly db As String = ConfigurationManager.AppSettings("DB")    Public Function CreateIStudent() As IStudent        Dim istudent As [Interface].IStudent        Dim className As String = "ChargeDAL" + "." + db + "StudentD"        istudent = CType(Assembly.Load(AssemblyName).CreateInstance(className), IStudent)        Return istudent    End FunctionEnd Class
Interface中的代码:

Public Interface IStudent

Function Register(enstudent As ChargeEntity.Student) As Integer

End Interface
ChargeDAL中的代码:

Imports System.Data
Imports System.Data.SqlClient
Imports ChargeDAL.SQLHelper
Imports [Interface]
Public Class StudentD
Implements [Interface].IStudent
'学生注册方法
Public Function Register(enstudent As ChargeEntity.Student) As Integer Implements [Interface].IStudent.Register
Dim strSQL As String
Dim regSqlHelper As New ChargeDAL.SQLHelper '实例化一个SQLhelper
Dim intReg As Integer
strSQL = "insert into student(cardID,name,stuID,sex,department,grade,class,balance,state,remark) values (@CardID,@name,@stuID,@sex,@department,@grade,@class,@balance,@state,@remark)" '添加到数据库
Dim sqlPara As SqlParameter() =
{
New SqlParameter("@CardID", enstudent.CardID),
New SqlParameter("@balance", enstudent.Balance),
New SqlParameter("@name", enstudent.Name),
New SqlParameter("@stuID", enstudent.StuID),
New SqlParameter("@sex", enstudent.Sex),
New SqlParameter("@department", enstudent.Department),
New SqlParameter("@grade", enstudent.Grade),
New SqlParameter("@class", enstudent.ClassID),
New SqlParameter("@state", enstudent.State),
New SqlParameter("@remark", enstudent.Remark)
}
intReg = regSqlHelper.ParaDataManager(strSQL, CommandType.Text, sqlPara) '调用sqlhelper的增删改(有参数)方法,返回一个integer,若是0则添加失败,1则是添加成功了1条记录
Return intReg
End Function
End Class
ChargeBLL层的代码:

'判断是否添加成功
Public Class RegisterManager
Public Function Register(enstudent As ChargeEntity.Student) As Boolean
Dim studentFactory As New ChargeFactory.studentFactory'定义一个新工厂
Dim istudent As [Interface].IStudent'定义一个接口
istudent = studentFactory.CreateIStudent()'实现工厂的创建接口方法,赋值于istudent

Dim intReg As Integer
intReg = istudent.Register(enstudent)'实现接口的Register方法,避免和D层打交道
If intReg = 0 Then
Return False
Else
Return True
End If

End Function
End Class
ChargeUI层的代码:

Public Class frmRegisterUI

Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim enstudent As New ChargeEntity.Student '定义一个实体
Dim regManager As New ChargeBLL.RegisterManager

enstudent.CardID = Trim(txtCardID.Text)
enstudent.StuID = Trim(txtStuID.Text)
enstudent.Name = Trim(txtName.Text)
enstudent.Balance = CDec(Trim(txtBalance.Text))
enstudent.Sex = cmbSex.Text
enstudent.Department = Trim(txtDepartment.Text)
enstudent.Grade = Trim(txtGrade.Text)
enstudent.ClassID = Trim(txtClassID.Text)
enstudent.Remark = Trim(txtRemark.Text)
enstudent.State = Trim(txtState.Text)
txtShowBalance.Text = txtBalance.Text

If regManager.Register(enstudent) = False Then '通过B层的判断,反馈注册信息。
MsgBox("注册失败")
Else
MsgBox("注册成功")
End If
End Sub
End Class
这样就实现了注册功能,外观模式还没加上去,就先不写了。