前言
七层的学习在一路的磕磕绊绊中也总算是告一段落了,由于没有看.net 视频,所以在使用.net敲的时候也是各种找不到思路,不知道如何下手,窗体建了删删了建,后来弄好之后所有代码敲完了开始调试,怎么都出不来,后来找了师傅才知道原来自己的窗体还是建错了,应该建类库,但是我却都是建的控制台应用程序,那就从头再来吧。
类库和控制台应用程序的区别
1、类库:类库编译出来的是dll文件,通常作为应用程序的库函数
2、控制台应用程序:控制台应用程序编译出来就是exe文件,可以之间运行,如图:
哪七层?
UI:显示给用户的界面,就是平时我们使用的软件的登录界面
facade:外观层通过UI层抽象出来的一个层,通过它来调用B层的东西,解耦合
BLL: 是UI层和DAL层的一个连接层
Factory:工厂里面有反射机制,通过反射机制来解除B层和D层的耦合
IDAL:抽象出来的D层中一个判断用户是否存在的方法
DAL:与数据库连接的层
sqlhelper:在七层中它是真正与数据库直接相连的层,降低耦合性
以下是各个层之间的调用关系图:
代码展示
UI
Imports System.Net.Dns
Public Class Form1
Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
Dim strUser As Boolean
Dim strPwd As Boolean
'定义一个外观对象
Dim FacadeLogin As New Facade.LoginFacade
'定义一个实体类对象
Dim UserInfo As New Entity.UserEntity
'判断文本框
If TxtUserName.Text = "" Then
MessageBox.Show("请输入用户名:", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'设置为空
TxtUserName.Text = ""
'用户获得焦点
TxtUserName.Focus()
Exit Sub
End If
If TxtPassWord.Text = "" Then
MessageBox.Show("请输入密码:", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
'设置为空
TxtPassWord.Text = ""
'密码框获得焦点
TxtPassWord.Focus()
Exit Sub
End If
'Try
'将文本框中的参数传递给实体
'将用户名和密码赋值给实体类的对象Userinfo UserName,Password属性
UserInfo.UserName = TxtUserName.Text.Trim()
'从而Userinfo获得了U层的参数,通过外观层,传递到B层进行判断
UserInfo.Pasword = TxtPassWord.Text.Trim()
'将U成的用户名通过外观层传到B层,得到外观的返回值
strUser = FacadeLogin.CheckUser(UserInfo)
strPwd = FacadeLogin.CheckPwd(UserInfo)
'通过返回值来判断用户是否存在
If strUser = False Or strPwd = False Then
MsgBox("用户名或密码不正确!")
TxtUserName.Text = ""
TxtPassWord.Text = ""
TxtUserName.Focus()
Else
MsgBox("登陆成功!")
End If
'Catch ex As Exception
' MsgBox("用户不存在或者密码不正确!")
' TxtUserName.Text = ""
' TxtPassWord.Text = ""
' TxtUserName.Focus()
'End Try
End Sub
Private Sub BtnCanCel_Click(sender As Object, e As EventArgs) Handles BtnCanCel.Click
'关闭窗体
Me.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'获取计算机名称
'classsystem.net.IPAddress提供网际协议(IP)地址
Dim Address() As System.Net.IPAddress
'class system.net.dns提供简单的域名解析功能
'GetHostName()获取本地主机名
'AddressList获取或设置与主机关联的IP列表
Address = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList
TxtComputer.Text = System.Net.Dns.GetHostName().ToString()
End Sub
End Class
facade
Imports System.Reflection
Public Class LoginFacade
Public Function CheckUser(ByVal UserInfo As Entity.UserEntity) As Boolean
'实例化B层UserBLL类对象
Dim isUserExists As New BLL.UserBLL()
Dim flag As Boolean
'判断是否存在该用户
flag = isUserExists.IsUserExists(UserInfo)
Return flag
End Function
Public Function CheckPwd(ByVal UserInfo As Entity.UserEntity) As Boolean
'实例化UserBLL对象
Dim isPwd As New BLL.UserBLL()
Dim flag1 As Boolean
'判断userinfo对象的密码是否存在
flag1 = isPwd.IsPwdRight(UserInfo)
Return flag1
End Function
End Class
BLL
Public Class UserBLL
'检查用户是否存在
Public Function IsUserExists(ByVal UserInfo As Entity.UserEntity) As Boolean
Dim Iuser As IDAL.IuserInfoDAL
'调用创建用户的工厂方法
Iuser = Factory.LoginFactory.CreateUserInfo
Dim table As DataTable
Dim flag As Boolean
'调用接口方法
table = Iuser.selectUser(UserInfo)
'由于在sqlhelper中以表格的形式存在(adataset.tables(0)),且开头第一列表示为0,所以Item(0)表示为用户名
'第一行第一列
If table.Rows(0).Item(0) = 0 Then
flag = False
Else
flag = True
End If
Return flag
End Function
'查看密码是否正确
Public Function IsPwdRight(ByVal UserInfo As Entity.UserEntity) As Boolean
Dim Iuser As IDAL.IuserInfoDAL
'中间变量来存储数据
Dim table As DataTable
Dim flag1 As Boolean
'调用工厂方法的createuserinfo来创建iuser
Iuser = Factory.LoginFactory.CreateUserInfo
'调用接口的方法selectuser
table = Iuser.selectUser(UserInfo)
If table.Rows(0).Item(0) = 0 Then
flag1 = False
Else
flag1 = True
End If
Return flag1
End Function
End Class
Factory
Imports System.Configuration
'使用反射来帮助我们克服抽象工厂模式的先天不足
Imports System.Reflection
Imports IDAL
Public Class LoginFactory
'读配置文件,D层的每个类在配置文件里面对应一个key
'下面这句是把key编程变量,然后在线面这个方法汇总用这个变量就可以应用D层里面这个类了
Dim conn As String = System.Configuration.ConfigurationSettings.AppSettings("ConnString")
''' <summary>
''' 实例化DAL中的UserDAL类
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function CreateUserInfo() As IDAL.IuserInfoDAL
'CType是一个内联函数,将前面的部分转换成后半部分
'LoginDAL为程序集名称
'Login.DAL为命名空间名称
'IuserInfoDAL为要实例化的“类名”
'CreateUserInfo为创建实例
Return CType(Assembly.Load("DAL").CreateInstance("DAL" & "." & "UserDAL"), IuserInfoDAL)
End Function
End Class
IDAL
Public Interface IuserInfoDAL
'UserInfo为用户的实体,是由实体类实例化而来的
'即所谓的传实体
'此接口定义了一个方法,用来检测用户是否存在
Function selectUser(ByVal UserInfo As Entity.UserEntity) As DataTable
End Interface
DAL
Imports System.Data.SqlClient
'system.data.sqlclinent命名空间是SQL SERVER的.netframework数据提供的程序
'sql server的.net framawork 数据提供程序描述了一个类集合,这个类用于访问托管中的SQL server数据库
Imports IDAL
Public Class UserDAL : Implements IDAL.IuserInfoDAL '实现接口中的方法
'声明并实例化sqlhelper类
Private SqlHelper As SQLHelper.sqlhelper = New SQLHelper.sqlhelper
Public Function selectUser(UserInfo As Entity.UserEntity) As DataTable Implements IuserInfoDAL.selectUser
Dim Sql As String
'中间变量用于存储从数据库中查找信息
Dim table As DataTable
'Dim sqlConnectStr As String = "server=localhost;database=Login;User ID=sa;pwd=123"
'声明并实例化参数数组
Dim sqlParams As SqlParameter() = {New SqlParameter("@UserName", UserInfo.UserName), New SqlParameter("@Password", UserInfo.Pasword)}
'SQL是查询到的信息
Sql = "select *from Users where UserName=@UserName and Password=@Password"
'下句为调用sqlhelper类中的getdatatable()方法来执行查询,并获取返回值
table = SqlHelper.GetDataTable(Sql, CommandType.Text, sqlParams)
Return table
End Function
End Class
sqlhelper
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data
Imports System.Reflection
Public Class sqlhelper
'configurationManager表示读配置文件
'appsetting获取配置文件的数据
'ConnString为配置文件中的key值
Public Shared strConn As String = ConfigurationManager.AppSettings("ConnString")
'sqldataadapter表示用于填充data.dataset和更新SQLserver数据库的一组数据命令和一个数据库连接
Private Shared Property adaptor As SqlDataAdapter
Public Shared Function GetDataTable(ByVal cmdTxt As String, ByVal cmdType As CommandType, ByVal paras As SqlParameter())
'建立数据库连接
Dim conn As SqlConnection = New SqlConnection(strConn)
'定义命令变量
Dim cmd As SqlCommand
'定义数据库适配器
Dim adaptor As SqlDataAdapter
'定义并实例化数据库缓冲区对象,即从数据库传入对象
Dim adataset As New DataSet
'在conn上实例化命令变量,并执行cmdType
cmd = New SqlCommand(cmdTxt, conn)
'执行命令的类型
cmd.CommandType = cmdType
'执行命令的参数
cmd.Parameters.AddRange(paras)
'将结果绑定到数据库适配器变量adaptor上面
adaptor = New SqlDataAdapter(cmd)
'dataset表示数据内存中缓存
adataset = New DataSet
Try
'如果数据库连接状态为关闭则将其打开
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
'向adaptor对象中填充要查询的数据
adaptor.Fill(adataset)
Catch ex As Exception
'错误处理程序,出错则提示
MsgBox(ex.Message, , "数据库操作!")
Finally
'如果连接状态为打开,则将其关闭,释放内存
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End Try
'以表格的形式返回结果
Return adataset.Tables(0)
End Function
'这是干嘛的?
Function GetDataTable() As DataTable
Throw New NotImplementedException
End Function
End Class
Entity
Public Class UserEntity
'区别变量的名字,变量的名字比数据库多“-”
Private _UserName As String
Private _Password As String
Private _UserID As String
Private _Lever As String
Private _Holder As String
'以下为可读写属性,名字和数据库中的一样
Public Property UserName() As String
Get
Return _UserName
End Get
Set(value As String)
_UserName = value
End Set
End Property
Public Property Pasword() As String
Get
Return _Password
End Get
Set(value As String)
_Password = value
End Set
End Property
Public Property UserID() As String
Get
Return _UserID
End Get
Set(value As String)
_UserID = value
End Set
End Property
Public Property Level() As String
Get
Return _Lever
End Get
Set(value As String)
_Lever = value
End Set
End Property
Public Property Holder() As String
Get
Return _Holder
End Get
Set(value As String)
_Holder = value
End Set
End Property
End Class
伴随着这个界面的出现,我也可以开始下面的学习了,加油!