VB.NET版“三层登录”实例
学习三层学习近一个月了,对于这块知识的学习,下面我已近期要做的项目机房收费系统的登录界面为例,检验一下我的三层学习:
实现功能:利用三层架构实现简单的用户登录,对于登录成功的用户要添加工作记录。
建立数据库:ComputerCharge_sys,该实例仅用到两张表
表T_User,包括字段:UserID,Password,Level,Status ……
表T_WorkLog,包括字段:UserID,Level,Status,LoginDate,LoginTime ……
Entity层代码实现:
创建实体层Entity程序集,创建封装用户信息类UserInfoEntity和用户工作日志类WorkLoginInfoEntity,该类不属于特定层次,而是专门用于封装数据,方便各层访问。该程序集被各层引用,但它不引用其它各层。
UserInfoEntity类封装表T_User返回的各字段。
'实体类UserInfoEntity:映射数据库表T_UserPublic Class UserInfoEntity Private _userID As String Private _level As String Private _status As String Private _password As String Public Property UserID() As String '属性用户ID Get Return _userID End Get Set(ByVal value As String) _userID = value End Set End Property Public Property Level() As String '属性用户权限 Get Return _level End Get Set(ByVal value As String) _level = value End Set End Property Public Property Status() As String '属性用户状态 Get Return _status End Get Set(ByVal value As String) _status = value End Set End Property Public Property Password() As String '属性用户密码 Get Return _password End Get Set(ByVal value As String) _password = value End Set End PropertyEnd Class
WorkLoginInfoEntity类封装表T_WorkLogo返回的各字段。
'实体类WorkLogInfoEntity:映射数据库表T_WorkLogPublic Class WorkLogInfoEntity Private _userID As String Private _level As String Private _loginDate As String Private _loginTime As String Private _status As String Public Property UserID() As String '属性用户ID Get Return _userID End Get Set(ByVal value As String) _userID = value End Set End Property Public Property Level() As String '属性用户权限 Get Return _level End Get Set(ByVal value As String) _level = value End Set End Property Public Property LoginDate() As String '属性登录日期 Get Return _loginDate End Get Set(ByVal value As String) _loginDate = value End Set End Property Public Property LoginTime() As String '属性登录时间 Get Return _loginTime End Get Set(ByVal value As String) _loginTime = value End Set End Property Public Property Status() As String '属性用户状态 Get Return _status End Get Set(ByVal value As String) _status = value End Set End PropertyEnd Class
DAL层代码实现:
''' <summary>''' 创建连接数据库类DbConnStr,方便之后各类连接数据库调用,对今后若更换数据库提供方便。''' </summary>''' <remarks></remarks>Public Class DbConnStr Public Shared Function connstring() As String connstring = "Server=192.168.24.168;Database=ComputerCharge_sys;User ID=sa;Password=123456" End FunctionEnd Class
Imports System.Data.SqlClientImports System.Data''' <summary>''' 创建数据访问对象UserDAL,访问表Users,返回符合条件调用''' </summary>''' <remarks></remarks>Public Class UserDAL ' 定义选择用户函数,按传入参数返回T_Users表中指定记录 Function SelectUser(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity Dim conn As New SqlConnection '创建连接对象 Dim cmd As New SqlCommand '创建命令对象 conn = New SqlConnection(DbConnStr.connstring()) '连接数据库 cmd.Connection = conn '根据实体中的用户名和密码选中记录内容 cmd.CommandText = "Select * from T_User Where UserID=@UserID and Password=@Password and Status='True'" '定义SQL语句 '添加命令参数 cmd.Parameters.Add(New SqlParameter("@UserID", user.UserID)) cmd.Parameters.Add(New SqlParameter("@Password", user.Password)) cmd.CommandType = CommandType.Text conn.Open() '打开连接 Dim reader As SqlClient.SqlDataReader reader = cmd.ExecuteReader() '执行查询 Dim users As New Entity.UserInfoEntity While (reader.Read()) '读取字段 If users Is Nothing Then users = New Entity.UserInfoEntity End If users.UserID = reader.GetString(reader.GetOrdinal("UserID")) users.Level = reader.GetString(reader.GetOrdinal("Level")) If (Not reader.IsDBNull(reader.GetOrdinal("Status"))) Then users.Status = reader.GetString(reader.GetOrdinal("Status")) End If End While conn.Close() Return users '返回查询结果 End FunctionEnd Class
Imports System.Data.SqlClient''' <summary>''' 创建数据库访问类工作日志WorkLogDAL,访问T_WorkLog,对于登录成功的用户插入工作日志记录。''' </summary>''' <remarks></remarks>Public Class WorkLogDAL Public Function FillWorkLog(ByVal userWorkLog As Entity.WorkLogInfoEntity) As Boolean Dim conn As SqlConnection Dim cmd As SqlCommand Dim connStr As String = "server=192.168.24.168;database=ComputerCharge_sys;uid=sa;pwd=123456 " conn = New SqlConnection(connStr) cmd = conn.CreateCommand cmd.CommandText = "Insert INTO T_WorkLog(UserID,Level,LoginDate,LoginTime,Status)values(@UserID,@Level,@LoginDate,@LoginTime,@Status)" cmd.Parameters.Add(New SqlParameter("@UserID", userWorkLog.UserID)) cmd.Parameters.Add(New SqlParameter("@Level", userWorkLog.Level)) cmd.Parameters.Add(New SqlParameter("@Status", userWorkLog.Status)) userWorkLog.LoginDate = Format(Now, "yyyy/MM/dd") userWorkLog.LoginTime = Format(Now, "HH:mm:ss") cmd.Parameters.Add(New SqlParameter("@LoginDate", userWorkLog.LoginDate)) cmd.Parameters.Add(New SqlParameter("@LoginTime", userWorkLog.LoginTime)) conn.Open() Dim num As Integer 'num,表示插入命令的执行结果 Dim flag As New Boolean 'flag,表示FillWorkLog()函数的返回结果 num = cmd.ExecuteNonQuery() '执行插入命令 '当num大于0,表示插入数据成功,否则失败。 If num > 0 Then flag = True Else flag = False End If conn.Close() Return flag End FunctionEnd Class
BLL层分析
''' <summary>''' 创建登录类,实现登录过程的逻辑处理''' </summary>''' <remarks></remarks>Public Class LoginBLL ' 定义登录函数,验证用户名和密码是否正确 Function UserLogin(ByVal user As Entity.UserInfoEntity) As Entity.UserInfoEntity Dim uDal As New DAL.UserDAL '引用DAL层,声明必要类 Dim users As New Entity.UserInfoEntity '引用Entity层,实例化UserInfoEntity类 Dim wDal As New DAL.WorkLogDAL '引用DAL层,实例化WorkLogDAL类 users = uDal.SelectUser(user) Return users End Function '定义添加工作日志函数 Function FillWorkLog(ByVal userWorkLog As Entity.WorkLogInfoEntity) As Boolean Dim worklog As New DAL.WorkLogDAL '实例化DAL层的WorkLogDAL类 Dim flag As Boolean 'flag用于定义DAL层的方法FillWorkLog的返回结果 flag = worklog.FillWorkLog(userWorkLog) Return flag End FunctionEnd Class
UI分析
1.创建系统登录界面:
2.编写登录事件代码:
Public Class LoginUI Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click Dim mgr As New BLL.LoginBLL '实例化业务逻辑层LoginBLL类 Dim user As New Entity.UserInfoEntity '实例化实体类UserInfoEntity Dim users As Entity.UserInfoEntity '实例化实体类UserInfoEntity,user:用于BLL层的方法UserLogin()返回的数据类型(实体类型)。 Dim enWorkLog As New Entity.WorkLogInfoEntity Dim workLog As New BLL.LoginBLL Dim flag As Boolean '将界面中的数据传入给实体中UserInfoEntity类的属性值 user.UserID = txtUserName.Text.Trim() user.Password = txtPassword.Text.Trim() '将界面中的数据传入给实体中UserInfoEntity类的属性值 enWorkLog.UserID = txtUserName.Text.Trim() '界面业务逻辑:判断用户名和密码不能为空 If txtUserName.Text = "" Then MessageBox.Show("用户名不能为空") Return End If If txtPassword.Text = "" Then MessageBox.Show("密码不能为空!") Return End If Try '引用BLL层,执行用户登录业务逻辑 users = mgr.UserLogin(user) If (users.UserID Is Nothing And users.Password Is Nothing) Then MessageBox.Show("登录失败,用户名和密码不匹配") Return Else enWorkLog.Level = users.Level enWorkLog.Status = users.Status '引用BLL层,执行添加工作日志业务逻辑 flag = mgr.FillWorkLog(enWorkLog) 'If (flag = False) Then ' MessageBox.Show("登录成功,插入数据失败") ' Return 'Else ' MessageBox.Show("插入数据成功") 'End If MessageBox.Show("登录成功! 登录用户:" + user.UserID) End If Catch ex As Exception MessageBox.Show(ex.Message()) End Try End SubEnd Class
此项目就此完成,系统模块如下图:
小结:三层架构用于用户业务逻辑复杂且需要存储数据的系统架构,其目的主要是降低系统之间的耦合,根据各层之间的引用,开发者可各层关注来予以各个层次的设计,明显改善系统的可维护性,增强系统优化的目的性。