上篇博客介绍了面向过程思维设计的登录界面,这篇文章就简单介绍一下我用三层架构思想实现的登录功能吧,还请高手多多指点,初次使用三层架构思想,使用难免有错误之处!
1.登录界面主要功能
此登录界面只是简单的实现了与数据库连接,登录成功时提示:‘登录成功!’,登录失败时提示:'登录失败!'。单击取消按钮可退出此系统。
2.数据库设计
根据功能介绍,此数据库设计比较简单,在此我只设计了一张表,跟上篇博客中使用的数据库相同,在此展现下数据库表的内容。
UserInfo数据表表结构,如图
我在数据表中添加的几条用户信息,如图
3.在vs中,的登录操作
运用三层架构思想来实现上机登录,不得不提提三层,看到下图了么,是不是对三层又有了深的认识。
三层架构各层之间的引用:
DAL所在程序集不引用BLL和UIBLL需要引用DALUI直接引用BLL,可能会间接引用DALDAL 、BLL、 UI 都需要引用Entity
三层架构的具体应用原则:
DAL只提供基本的数据访问,不包含任何业务相关的逻辑处理;UI只负责显示和采集用户操作,不包含任何的业务相关的逻辑处理;BLL负责处理业务逻辑。通过获取UI传来的操作指令,决定执行业务逻辑,在需要访问数据源的时候直接交给DAL处理。处理完成后,返回必要数据给UI。下面说一下中登录的操作
看到这幅图了嘛,下面咱来说说各层之间的具体实现。
①对UI显示层,界面设计如图
"UI显示层"代码设计
Public Class UserLogin'单击确定按钮,判断登录成功与否。Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.ClickDim LoginUser As New LoginEntity.e_UserInfo'定义一个LoginEntity.e_UserInfo对象Dim Bcheck As New LoginBLL.LoginManager '定义一个LoginBLL.LoginManager对象LoginUser.UserID = txtUserName.Text()LoginUser.Password = txtPassword.Text()'采集用户信息,展现登录结果If Bcheck.SelectUIandEntity(LoginUser) ThenMsgBox("登录成功!")ElseMsgBox("登录失败!")End IfEnd Sub'退出系统Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.ClickEndEnd SubEnd Class
②"业务逻辑层"代码设计
Imports LoginEntityImports LoginDAL.UserDAO'B层业务处理(逻辑),做判断Public Class LoginManagerFunction SelectUIandEntity(ByVal User As LoginEntity.e_UserInfo) As BooleanDim DalUser As New LoginDAL.UserDAODim EntityUser As New LoginEntity.e_UserInfoEntityUser.UserID = User.UserIDEntityUser = DalUser.selectUserInfoFormUserInfoTable(EntityUser)'判断操作If EntityUser.Password = User.Password ThenReturn TrueElseReturn FalseEnd IfEnd FunctionEnd Class
③"数据访问层"代码设计
'引用命名空间Imports System.Data.SqlClientImports LoginEntityPublic Class UserDAO'连接数据库Dim sqlConnectStr As String = "server=waly-pc;database=Login;uid=sa;pwd=123456;"Dim sqlConnection1 As SqlConnection = New SqlConnection(sqlConnectStr)'自定义检查参数Function selectUserInfoFormUserInfoTable(ByVal User As LoginEntity.e_UserInfo) As LoginEntity.e_UserInfoDim sql As String = "select * from UserInfo where UserID='" & User.UserID & "'"'sql语句Dim cmd As SqlCommand = New SqlCommand(sql, sqlConnection1)'定义sqlCommand对象Dim read As SqlDataReader '定义sqlDataReader对象Dim UserDataTable As New DataTable '定义一个DataTable对象Dim User1 As New LoginEntity.e_UserInfoTrysqlConnection1.Open()read = cmd.ExecuteReader()UserDataTable.Load(read)User1.UserID = UserDataTable.Rows(0)("UserID") '感觉这一行和下一行代码应该写到B层的,可是写过去总是出错User1.Password = UserDataTable.Rows(0)("UserPassword")Return User1Catch ex As ExceptionUser1.Password = ""Return User1FinallyIf Not IsNothing(sqlConnection1) ThensqlConnection1.Close()End IfEnd TryEnd FunctionEnd Class
④"实体层"代码设计
'引用命名空间Imports System.Collections.GenericImports System.LinqImports System.Text'声明用户的两个属性值:UserID 和 PasswordPublic Class e_UserInfoPrivate e_UserID As StringPublic Property UserID As StringGetReturn e_UserIDEnd GetSet(ByVal value As String)e_UserID = valueEnd SetEnd PropertyPrivate e_Password As StringPublic Property Password As StringGetReturn e_PasswordEnd GetSet(ByVal value As String)e_Password = valueEnd SetEnd PropertyEnd Class
注:此Demo源码以上传至:用户登录(三层架构思想)