前言
浑浑噩噩中开始了机房的重构,项目开始快大半个月了依旧懒懒散散不知干嘛,参考好几个人的博客写出来的配置文件和登录程序依旧无法运行通过。请来各种大神各种调错,但是依旧是错误百出。那就模仿一个人的思路吧。找来了博客写的比较全的同学的博客,一遍敲一遍理解。终于懂了点。
内容
首先我们来看一下机房重构的包图
(摘自网上)
然后说一下我对七层的理解
从三层到七层的变化 最多的是多了一个外观层(Facade)和一个工厂层(Factory)另外多了一个接口。而这几个层都有各自的只能。但是总的核心就是:“解耦”。
像图中所画的,被指向的模块就是被引用的,一层一层的引用就让层与层之间的关系没有那么强的依赖。如果我们要改动界面,那么只需要改动UI层的代码即可。
使用外观模式也是为了让UI层和业务逻辑层更好的解耦。如需改动业务逻辑那么只需要改动BLL层即可。工厂模式的使用也是为了方便更换数据库。据我们所知现在的SQLserver只是中小企业应用的数据库,大点的公司可能就要用到Mysql和Oracle,为了节约时间节约成本我们必须要设计出可以方便的更换数据库的代码,这就需要我们的设计模式中的工厂模式了。但是每一层都是需要实体层的。实体层其实就是放数据库字段的一个的集合。这里放着该功能所需要表中的所有字段。除了设计模式的使用在七层中我们还添加了SQLHelper具体代码看下面
最后:
看一下我的代码(其实是洪玉的)
UI层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports Facade
Imports Model.UserModel
Imports System.Net.Dns
Imports System.Windows.Forms
Public Class frmLogin
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim UserInfo As New Model.UserModel
Dim fac As New Facade.LoginFacade
Dim strResult1 As Boolean
Try
UserInfo.UserID = txtUserID.Text.Trim()
UserInfo.Password = txtPassword.Text.Trim()
strResult1 = fac.CheckUser(UserInfo)
If strResult1 = False Then
MsgBox("用户不存在")
txtUserID.Text = ""
txtPassword.Text = ""
txtUserID.Select()
txtUserID.Focus()
End If
Dim table As DataTable
table = fac.CheckPwd(UserInfo)
If txtPassword.Text.Trim = Trim(table.Rows(0).Item(1)) Then
MsgBox("登录成功")
frmMain.Show()
End If
Catch ex As Exception
MsgBox("用户不存在或者密码不正确!")
txtPassword.Text = ""
txtUserID.Text = ""
txtUserID.Focus()
End Try
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
End
End Sub
Private Sub frmLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtUserID.Select()
txtUserID.Focus()
End Sub
End Class</span>
Facade 层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports Model
Imports BLL
Imports System.Reflection
Public Class LoginFacade
Public Function CheckUser(ByVal UserInfo As Model.UserModel) As Boolean
Dim IsUsers As New BLL.LoginBLL()
Dim flag As Boolean
flag = IsUsers.IsUserExits(UserInfo)
Return flag
End Function
Public Function CheckPwd(ByVal UserInfo As Model.UserModel) As DataTable
Dim IsPwd As New BLL.LoginBLL()
Dim table As DataTable
table = IsPwd.IsPWDRight(UserInfo)
Return table
End Function
End Class</span>
BLL层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports IDAL
Imports Model
Public Class LoginBLL
'查看用户和是否存在
Public Function IsUserExits(ByVal UserInfo As Model.UserModel) As Boolean
Dim fac As New Factory.LoginFactory()
Dim Iuser As IDAL.ILoginDAL
Iuser = fac.CreateUserInfo
Dim table As DataTable
Dim flag As Boolean
table = Iuser.SelectUser(UserInfo)
If table.Rows(0).Item(0) Then '第一行第一列
flag = True
Else
flag = False
End If
Return flag
End Function
'查看密码是否正确
Public Function IsPWDRight(ByVal UserInfo As Model.UserModel) As DataTable
Dim Iuser As IDAL.ILoginDAL
Dim dt As DataTable
Dim fac As New Factory.LoginFactory
Iuser = fac.CreateUserInfo
dt = Iuser.SelectUser(UserInfo)
Return dt
End Function
End Class</span>
Factory 层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Reflection
Imports System.Configuration
Imports IDAL
Public Class LoginFactory
' Private Shared ReadOnly AssemblyName As String = "DAL"
Dim strDB As String = System.Configuration.ConfigurationManager.AppSettings("DB")
'创建用户信息验证的接口方法
Public Function CreateUserInfo() As IDAL.ILoginDAL
' Dim ClassName As String = AssemblyName + "." + strDB + "serverUserDAL"
' Return CType(Assembly.Load(AssemblyName).CreateInstance(ClassName), IDAL.IUserDAO)
Return CType(Assembly.Load("DAL").CreateInstance("DAL.LoginSQL"), IDAL.ILoginDAL)
End Function
End Class</span>
IDAL层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Reflection
Public Interface ILoginDAL
Function SelectUser(ByVal UserInfo As Model.UserModel) As DataTable
End Interface
</span>
DAL层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports Model
Imports IDAL
Imports System.Data.SqlClient
Imports SQLHelper
Public Class LoginSQL : Implements IDAL.ILoginDAL
Public Function SelectUser(UserInfo As UserModel) As DataTable Implements ILoginDAL.SelectUser
Dim shelper As New SQLHelper.SqlHelper
Dim paras As SqlParameter() = {New SqlParameter("@UserID", UserInfo.UserID), New SqlParameter("@Password", UserInfo.Password)}
Dim cmdText As String
cmdText = "SELECT * FROM User_info where UserID=@UserID And Password=@Password"
Dim dt As DataTable
dt = shelper.ExecSelect(cmdText, CommandType.Text, paras)
Return dt
End Function
End Class
</span>
Model层
<span style="font-family:KaiTi_GB2312;font-size:18px;">Public Class UserModel
Private _UserID As String
Private _Password As String
Private _Level As String
Private _UserName As String
Private _Head As String
Public Property UserName() As String
Get
Return _UserName
End Get
Set(value As String)
_UserName = 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 Password() As String
Get
Return _Password
End Get
Set(value As String)
_Password = value
End Set
End Property
Public Property Level() As String
Get
Return _Level
End Get
Set(value As String)
_Level = value
End Set
End Property
Public Property Head() As String
Get
Return _Head
End Get
Set(value As String)
_Head = Level
End Set
End Property
End Class
</span>
SQLHelper
<span style="font-family:KaiTi_GB2312;font-size:18px;">Imports System.Data.SqlClient
Imports System.Configuration
Public Class SqlHelper
Dim DBConnectStr As New DALUtil
Dim DBConnection As New SqlConnection(DBConnectStr.connectString)
Dim cmd As New SqlCommand
'设置连接
'定义cmd命令
Public Function ExecAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter())
'将传入的值,分别为cmd的属性赋值
cmd.Parameters.AddRange(sqlParams) '将参数传入
cmd.CommandType = cmdType
cmd.Connection = DBConnection
cmd.CommandText = cmdText
Try
DBConnection.Open()
Return cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Catch ex As Exception
Return 0
Finally
Call CloseConn(DBConnection)
Call CloseCmd(cmd)
End Try
End Function
Public Function ExecAddDelUpdate(ByVal cmdText As String, ByVal cmdType As CommandType) As Integer
'为要执行的命令cmd赋值
cmd.CommandText = cmdText '先是查询的sql语句
cmd.CommandType = cmdType '设置Sql语句如何解释
cmd.Connection = DBConnection '设置连接
'执行操作
Try
DBConnection.Open()
Return cmd.ExecuteNonQuery()
Catch ex As Exception
Return 0
Finally
Call CloseConn(DBConnection)
Call CloseCmd(cmd)
End Try
End Function
Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable
'执行查询操作,有参数
Dim adapter As SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
cmd.CommandText = cmdText
cmd.CommandType = cmdType
cmd.Connection = DBConnection
cmd.Parameters.AddRange(sqlParams)
adapter = New SqlDataAdapter(cmd)
Try
adapter.Fill(ds)
dt = ds.Tables(0)
cmd.Parameters.Clear()
Catch ex As Exception
Throw New Exception("查询失败!")
Finally
Call CloseCmd(cmd)
End Try
Return dt
End Function
Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType) As DataTable
'执行查询操作,无参数
Dim adapter As SqlDataAdapter
Dim ds As New DataSet
cmd.CommandText = cmdText
cmd.CommandType = cmdType
cmd.Connection = DBConnection
adapter = New SqlDataAdapter(cmd)
Try
adapter.Fill(ds)
Return ds.Tables(0)
Catch ex As Exception
Return Nothing
Finally
Call CloseCmd(cmd)
End Try
End Function
Public Sub CloseConn(ByVal conn As SqlConnection)
If (conn.State <> ConnectionState.Closed) Then '如果没有关闭
conn.Close() '关闭连接
conn = Nothing '不指向原对象
End If
End Sub
Public Sub CloseCmd(ByVal cmd As SqlCommand)
If Not IsNothing(cmd) Then '如果cmd命令存在
cmd.Dispose() '销毁
cmd = Nothing
End If
End Sub
End Class
</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"> Private ReadOnly db As String = System.Configuration.ConfigurationManager.AppSettings("DBString") '数据库
Private ReadOnly ConnectStr As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnString").ToString
'sql数据库连接字符串
Public connectString As String = ConnectStr
</span>
另附配置文件:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings >
<add name ="ConnString" connectionString ="Data Source=HAOZHANGJING\SQL2012;Initial Catalog=charge;UID=sa;PWD=1"/>
</connectionStrings>
<appSettings >
<clear/>
<add key ="DBString" value ="Sql server"/>
</appSettings>
</configuration></span>
SQLHelper请参照李晓洁的博客 SQLHelper
整个机房重构请参照王洪玉 机房重构华丽转身
总结:
期待更完美的自己!如有不足或者意见欢迎指出。感谢阅读