七层,顾名思义就是把一个窗体分层七块,上篇博客我们分析了三层,那么七层也就是在三层的基础上,再加上四层。
这入住新家的四层是什么呢?这个就可以因人而异了,因为我们之前学过大话,那么这四层,我们就可以从大话借了,因为我比较不聪明,所以我就遵循常规,和大多数一样用最常见的七层了。
先看看我的七层有什么。
接着看他们的关系
如图,可知,我们首先要解决的依旧是实体层。
- Public Class User
- Private name As String
- Private password As String
- Private Userid As String
- Private level As String
- Private account As String
- '以下为可读写属性
- Public Property user_name() As String
- Get
- Return name
- End Get
- Set(ByVal value As String)
- name = value
- End Set
- End Property
- Public Property user_id() As String
- Get
- Return Userid
- End Get
- Set(ByVal value As String)
- Userid = value
- End Set
- End Property
- Public Property user_pwd() As String
- Get
- Return password
- End Get
- Set(ByVal value As String)
- password = value
- End Set
- End Property
- Public Property user_level() As String
- Get
- Return level
- End Get
- Set(ByVal value As String)
- level = value
- End Set
- End Property
- Public Property user_account() As String
- Get
- Return account
- End Get
- Set(ByVal value As String)
- account = value
- End Set
- End Property
- End Class
- Public Interface IUserInfo
- '查询用户的接口函数
- Function QueryUserInfo(ByVal user As Entity.UserInfo) As Entity.UserInfo
- End Interface
- Public Class T_UserDAL : Implements IUser
- '/// <summary>
- '/// depiction:<查询用户名和密码是否正确>
- '/// </summary>
- '/// <param name="<enUser>"><用户实体></param>
- '/// <returns>
- '///<返回一个用户实体的集合>
- '/// </returns>
- Public Function SelectUser(ByVal enUser As Entity.UserEntity) As List(Of Entity.UserEntity) Implements IUser.SelectUser
- Dim strText As String = "select * from T_User where UserID=@UserID and password=@password" 'sql语句
- Dim cmdType As CommandType = CommandType.Text '命令类型
- Dim Parameter As SqlParameter()
- '传参
- Parameter = {New SqlParameter("@UserID", enUser.userID),
- New SqlParameter("@password", enUser.password)}
- Dim SqlHelper As New SqlHelper '实例化SqlHelper这个类的一个对象
- Dim dt As New DataTable
- Dim myList As List(Of Entity.UserEntity)
- dt = SqlHelper.ExecuteReaderTable(strText, cmdType, Parameter) '调用sqlhelper中executereadertable的方法
- myList = EntityHelper.convertToList(Of Entity.UserEntity)(dt)
- Return myList
- End Function
- End Class
然后就是和他紧密联系的设计模式。工厂141页,不懂看看(我也不太懂,呵呵、)
- Imports System.Reflection
- Public Class DataAccess
- '/// <summary>
- '/// depiction:<创建用户接口>
- '/// </summary>
- '/// <param name="<>"><></param>
- '/// <returns>
- '///<返回IUserDAL>
- '/// </returns>
- Public Function CreateIUser() As IDAL.IUser
- Return CType(Assembly.Load("DAL").CreateInstance("DAL.T_UserDAL"), IDAL.IUser)
- End Function
- End Class
- Public Function SelectUser(ByVal enUser As Entity.UserEntity) As List(Of Entity.UserEntity)
- Dim factory As New Factory.DataAccess
- Dim IUser As IDAL.IUser
- Dim myList As List(Of Entity.UserEntity)
- IUser = factory.CreateIUser()
- myList = IUser.SelectUser(enUser)
- Return myList
- End Function
- Public Function SelectUser(ByVal enUser As Entity.UserEntity) As List(Of Entity.UserEntity)
- Dim userBLL As New BLL.T_UserBLL
- Dim myList As List(Of Entity.UserEntity)
- myList = userBLL.SelectUser(enUser)
- If myList.Count = 0 Then
- Throw New Exception("用户名或密码输入错误")
- Else
- Return myList
- End If
- End Function
- Private Sub btnConfirm_Click(sender As Object, e As EventArgs) Handles btnConfirm.Click
- Dim facade As New Facade.LoginFacade
- Dim myList As New List(Of Entity.UserEntity)
- Dim enUser As New Entity.UserEntity '封装实体
- Dim flag As Boolean
- Try
- enUser.userID = txtUserName.Text.Trim()
- enUser.password = txtPassword.Text.Trim()
- myList = facade.SelectUser(enUser)
- If myList.Count > 0 Then
- MsgBox("登录成功")
- Dim enWorklog As New Entity.WorklogEntity
- enWorklog.userID = txtUserName.Text.Trim()
- enWorklog.loginDate = CStr(Format(Now(), "yyyy-MM-dd"))
- enWorklog.loginTime = CStr(Format(Now(), "HH:mm:ss"))
- enWorklog.status = "正在值班"
- enWorklog.computer = Environment.GetEnvironmentVariable("USERNAME")
- flag = facade.InsertWorklog(enWorklog)
- enLogin.userID = myList.Item(0).userID
- enLogin.level = myList.Item(0).level
- End If
- Catch ex As Exception
- MessageBox.Show(ex.Message.ToString())
- txtUserName.Focus()
- txtUserName.SelectAll()
- txtPassword.Text = ""
- End Try
- End Sub
- Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
- Me.Close()
- End Sub