每当别人问我关于三层的知识的时候我总是推荐自己的《三层架构之抽象工厂加反射》这篇博客。这篇博客是自己当时学习三层的时候写的一个简单的登录例子。这个例子通过登录这个用例简单的介绍了三层,然后过度的三层+简单工厂再过渡到三层+抽象工厂。不管说从方法命名或者代码风格上来说自己都不敢说是最好的,但是体现三层我感觉还是绰绰有余的。然而最近在给别人讲述三层时候被他人指出这篇博客的代码有些许错误,虽然别人指出的方式有些令自己不快,甚至气愤!但是毕竟还有人关注自己,还有人看自己的博客,还有人给自己指出不足,从这个方面来看这还应该算是好事。有错误就改!有错误才能进步嘛。也许这就是米老师总是说的教学相长吧,这次终于体会到了,而且体会很深刻!!!
真的是“知不足”了,真的是想“自强”了,为了验证代码的正确性决定将这个登录的小例子又从最简单的三层开始做起逐步增加难度(降低耦合度),重新实现了一遍。再一次用了用VB.NET,再次回到了傻瓜式的VS环境中。
代码如下:
简单三层
界面层
Public Class Login
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim LUser As New Entity.User
Dim BCheck As New Bll.BllLogin
LUser.User_Id = txtUserId.Text
LUser.User_Pwd = txtPwd.Text
If BCheck.Check(LUser) = True Then
MsgBox("登录成功!")
Else
MsgBox("登录失败!")
End If
End Sub
Private Sub btnCancle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancle.Click
End
End Sub
End Class
BLL层
Public Class BllLogin
Function Check(ByVal User As Entity.User) As Boolean
Dim DaUser As New DAL.DalUserInfo
Dim BlUser As New Entity.User
BlUser.User_Id = User.User_Id
BlUser = DaUser.Check(BlUser)
If BlUser.User_Pwd = User.User_Pwd Then
Return True
Else
Return False
End If
End Function
End Class
DAL层
Imports System.Data.SqlClient
Public Class DalUserInfo
Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"
Dim conn As SqlConnection = New SqlConnection(ConnStr)
Function Check(ByVal User As Entity.User) As Entity.User
Dim sql As String = "select * from UserInfo where User_ID='" & User.User_Id + "'"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
Dim read As SqlDataReader
Try
conn.Open()
read = cmd.ExecuteReader
read.Read()
User.User_Id = read.Item("User_ID")
User.User_Pwd = read.Item("User_Pwd")
Return User
Catch ex As Exception
User.User_Pwd = ""
Return User
End Try
End Function
End Class
实体层
Public Class User
Private userId As String
Private userPwd As String
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 userPwd
End Get
Set(ByVal value As String)
userPwd = value
End Set
End Property
End Class
三层+简单工厂
界面层代码不变
Bll层
Public Class BllLogin
Function Check(ByVal User As Entity.User) As Boolean
Dim DalFactory As New DAL.DFactory
Dim BlUser As New Entity.User
BlUser.User_Id = User.User_Id
BlUser = DalFactory.CreateUserInfo.Check(BlUser)
If BlUser.User_Pwd = User.User_Pwd Then
Return True
Else
Return False
End If
End Function
End Class
工厂
Imports [Interface]
Public Class DFactory
'Dim DataBase As String = "Access"
Dim DataBase As String = "Sql"
Function CreateUserInfo() As IUserInfo
Dim DB As IUserInfo
Select Case DataBase
Case "Sql"
DB = New DalUserInfo
'Case "Access"
' DB = New D_UserInfoAccess
End Select
Return DB
End Function
End Class
接口
Public Interface IUserInfo
Function Check(ByVal IUser As Entity.User) As Entity.User
End Interface
DAL层
Imports System.Data.SqlClient
Public Class DalUserInfo : Implements [Interface].IUserInfo
Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"
Dim conn As SqlConnection = New SqlConnection(ConnStr)
Public Function Check(ByVal IUser As Entity.User) As Entity.User Implements [Interface].IUserInfo.Check
Dim sql As String = "select * from UserInfo where User_ID='" & IUser.User_Id + "'"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
Dim read As SqlDataReader
Try
conn.Open()
read = cmd.ExecuteReader
read.Read()
IUser.User_Id = read.Item("User_ID")
IUser.User_Pwd = read.Item("User_Pwd")
Return IUser
Catch ex As Exception
IUser.User_Pwd = ""
Return IUser
End Try
End Function
End Class
三层+抽象工厂
界面层代码不变
Bll层代码不变
接口代码不变
工厂代码
Imports [Interface]
Imports System.Reflection
Public Class DFactory
'Dim DataBase As String = "Access"
'Dim DataBase As String = "Sql"
Dim strDB As String = System.Configuration.ConfigurationSettings.AppSettings("DBString")
Function CreateUserInfo() As IUserInfo
Return CType(Assembly.Load("DAL").CreateInstance("DAL.DalUserInfo" & strDB), IUserInfo)
End Function
End Class
DAL层
Imports System.Data.SqlClient
Public Class DalUserInfoSql : Implements [Interface].IUserInfo
'Dim ConnStr As String = "Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"
Dim strConnStr As String = System.Configuration.ConfigurationSettings.AppSettings("ConnStr")
Dim conn As SqlConnection = New SqlConnection(strConnStr)
Public Function Check(ByVal IUser As Entity.User) As Entity.User Implements [Interface].IUserInfo.Check
Dim sql As String = "select * from UserInfo where User_ID='" & IUser.User_Id + "'"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
Dim read As SqlDataReader
Try
conn.Open()
read = cmd.ExecuteReader
read.Read()
IUser.User_Id = read.Item("User_ID")
IUser.User_Pwd = read.Item("User_Pwd")
Return IUser
Catch ex As Exception
IUser.User_Pwd = ""
Return IUser
End Try
End Function
End Class
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
</startup>
<appSettings>
<add key="ConnStr" value ="Data Source=192.168.24.186;Initial Catalog=Student;User ID=sa;Pwd=123456"></add>
<add key="DBString" value ="Sql"></add>
</appSettings>
</configuration>
对于知识的认知真的是每次都有不同的感受,以前的目标只是实现,无论怎么样只要实现就好,有很多地方都是不理解的,现在再去看那些代码感觉理解起来轻松了很多。敲代码的过程中感触最大的就是java和net平台的区别。通过这段时间做DRP的经历明显感觉net简直是傻瓜化的操作,而java操作起来就要烦人的多,也许现在还是对java的IDE不太熟悉吧。
作者:beijiguangyong 发表于2012-1-14 1:11:25
原文链接