机房重构已经拖了很长时间了,需要做一些总结来整理自己的思路,由于自己思路不清所以在机房重构的路上走的好慢。于是自己的机房有很多的bug,借助文文同学的帮助,自己的增加用户这条线终于走通,相信自己其余的功能bug也会慢慢挑出来。
第一步:实体层:EntityUserInfo:用户实体的属性。
Public ClassEntityUserInfo Private_userID As String Private_password As String Private_username As String Private_level As String Private_head As String PublicPropertyUserIDAs String Get Return_userID EndGet Set(valueAsString) _userID = value EndSet EndProperty PublicPropertyPasswordAs String Get Return_password EndGet Set(valueAsString) _password = value EndSet EndProperty PublicPropertyLevelAs String Get Return_level EndGet Set(valueAsString) _level = value EndSet EndProperty PublicPropertyUserNameAs String Get Return_username EndGet Set(valueAsString) _username = value EndSet EndProperty PublicPropertyHeadAs String Get Return_head EndGet Set(valueAsString) _head = value EndSet EndProperty End Class
第二步:接口层:主要是为了把B层和D层建立联系
Imports Entity Public InterfaceIUserInfo FunctionAddUser(ByVal UserAsEntityUserInfo)As Integer End Interface
第三步:D层:实现接口定义的方法
Imports IDAL Imports System.Data.SqlClient Imports Entity Imports DAL.SqlHelper Public ClassStudentInfoDAO :Implements IDAL.IStudentInfo Private SQLHelper AsDAL.SqlHelper = NewDAL.SqlHelper() Public FunctionAddUser(UserAs EntityUserInfo)AsIntegerImplementsIUserInfo.AddUser Dimsql As String Dimresult As Integer Dimsqlparams As SqlParameter()= {NewSqlParameter("@UserID", User.UserID),NewSqlParameter("@UserLevel",User.Level),New SqlParameter("@Password", User.Password),NewSqlParameter("@UserName",User.UserName),New SqlParameter("@Head", User.Head)} sql ="insert into T_User_Info (UserID,Level,Password,UserName,Head) values(@UserID,@UserLevel,@Password,@UserName,@Head)" result = SQLHelper.ExecAddDelUpdate(sql, CommandType.Text,sqlparams) Returnresult End Function
第四步:工厂层:把D层的数据操作封装到工厂层,通过调用SQLHelper来实现数据库数据的增、删、改、查
Imports System.Configuration Imports System.Reflection Imports IDAL Public ClasssqlFactory PrivateSharedReadOnlyAssemblyNameAs String= "DAL" PrivateSharedReadOnlydbAs String= ConfigurationManager.AppSettings("DB") ''' <summary> '''用户工厂 '''</summary> '''<remarks></remarks> PublicFunctionCreateIUser()As IUserInfo DimClassName As String= "DAL" + "."+ db + "UserInfoDAO" DimUser As IUserInfo User= CType(Assembly.Load("DAL").CreateInstance(ClassName),IUserInfo) ReturnUser EndFunction End Class
第五步:B层:把D层的数据库操作,转换成逻辑的运算或者判断
Imports Factory Imports Entity Imports IDAL Public ClassB_UserInfo Public FunctionBLLaddUser(ByVal UserAsEntity.EntityUserInfo)AsBoolean DimI_addUser As IDAL.IUserInfo Dimfactory As NewFactory.sqlFactory Dimresult As Boolean I_addUser = factory.CreateIUser() result = I_addUser.AddUser(User) Ifresult = True Then ReturnTrue Else ReturnFalse EndIf EndFunction End Class
第六步:外观层:为了不让B层的操作影响到UI层,解除它们之间的耦合度。
Imports Entity Imports BLL Public ClassfaUserInfo Public FunctionfAddUser(ByVal UserAsEntity.EntityUserInfo)AsBoolean DimbllAddUser As NewBLL.B_UserInfo Dimresult As Boolean result = bllAddUser.BLLaddUser(User) Ifresult = True Then ReturnTrue Else ReturnFalse EndIf End Function End Class
第七步:UI层:显示传回的数据或者输入传入的数据。
Public ClassfrmAddUser PrivateSubfrmAddUser_Load(senderAs Object,e As EventArgs)HandlesMyBase.Load comLevel.Items.Add("管理员") comLevel.Items.Add("操作员") comLevel.Items.Add("一般用户") txtUserName.MaxLength = 12 txtUserID.MaxLength = 12 txtPassword.MaxLength = 12 txtOkPWD.MaxLength = 12 EndSub PrivateSubbtnOK_Click(senderAs Object,e As EventArgs)HandlesbtnOK.Click IftxtUserName.Text = "" Then MsgBox("请输入用户姓名", 0,"提示") ExitSub ElseIftxtUserID.Text = "" Then MsgBox("请输入用户名", 0,"提示") ExitSub ElseIfcomLevel.Text = "" Then MsgBox("请选择用户等级", 0,"提示") ExitSub ElseIftxtPassword.Text = "" Then MsgBox("请输入用户密码", 0,"提示") ExitSub ElseIftxtOkPWD.Text = "" Then MsgBox("请再次确认您的密码", 0,"提示") ExitSub EndIf IftxtPassword.Text <> txtOkPWD.TextThen MsgBox("俩次密码输入不一致,请重新输入", 0,"提示") ExitSub Else DimeUser As NewEntity.EntityUserInfo Dimresult As Boolean DimfselectUserID As NewFacade.faUserInfo eUser.UserID = Trim(txtUserID.Text) result = fselectUserID.fselectUserUserID(eUser) Ifresult = True Then MsgBox("该用户已经存在,重新输入用户名", 0,"提示") txtUserID.Text = "" txtUserID.Select() txtUserID.Focus() Else eUser.UserName = Trim(txtUserName.Text) eUser.UserID = Trim(txtUserID.Text) eUser.Level = comLevel.Text eUser.Password = Trim(txtPassword.Text) eUser.Head = Trim(frmLogin.txtUserName.Text) Dim fAddUser AsNewFacade.faUserInfo result = fAddUser.fAddUser(eUser) If result = TrueThen MsgBox("添加成功", 0,"提示") Else MsgBox("添加失败", 0,"提示") End If EndIf EndIf EndSub PrivateSubbtnExit_Click(senderAs Object,e As EventArgs)HandlesbtnExit.Click frmMain.Show() Me.Close() EndSub End Class
上边讲诉的大概就是从我们知道需求,得到实体之后要进行的一系列实现的步骤,那么数据到底是怎么运行的!
下面是数据运行的流程(功能实现流程):UI----> Façade---->BLL---->Factory---->BLL---->Façade---->UI
工厂中进行的操作就是:向数据库中添加数据的或者查询数据库的操作。
BLL---->Factory---->DAL---->Factory---->BLL
第一部分是“已知数据---->需求——>功能实现”,第二部分“功能实现---->数据---->需求”,这俩部分大概就是开发人员整体的俩条思路。
这个增加用户的功能实现了,那么删除和查询就更简单的多了,其实机房中就是这三个操作,至于更新就是它们的基础,对于这三个操作给予一定的响应。机房里满满的都是收获,我觉得调Bug是我们学到更多得好机会,遇到问题解决问题,收获颇丰。