码迷,mamicode.com
首页 > 其他好文 > 详细

三层架构-----实践篇

时间:2014-06-07 16:29:14      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:vb.net   三层   设计   

上篇论述了三层的基本概念,作用,关系,现在展示下系统中用户登陆过程中简单应用三层结构设计思想。

      vb.NET的操作如下

       首先建立以下windows应用程序以及类库

               bubuko.com,布布扣

  首先建立实体类

        

Public Class userInfo
    Private _username As String
    Public Property UserName As String
        Get
            Return _username

        End Get
        Set(ByVal value As String)
            _username = value

        End Set
    End Property
    Private _pwd As String
    Public Property Pwd As String
        Get
            Return _pwd

        End Get
        Set(value As String)
            _pwd = value
        End Set
    End Property
End Class
UI层,前提是引用实体类(model)和BLL

   

Public Class Form1

    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Try
            Dim euser2 As New Model.userInfo
            Dim euser3 As New Model.userInfo

            euser2.UserName = txtUserName.Text.Trim '将U层数据传递给实体
            euser2.Pwd = txtPassword.Text.Trim

            Dim mgr As New BLL.LoginMannager '将数据传递给B层
            euser3 = mgr.UserLogin(euser2)
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())

        End Try
    End Sub
End Class

  DAL层:前提引用实体类

Imports System.Data
Imports System.Data.SqlClient
Imports Model

Public Class user
    '创建数据库链接
    Public conn As New SqlConnection("server=LXY;database=login;user Id=sa;password=123456")
    Public Function SelectUser(ByVal User As Model.userInfo) As Model.userInfo
        '传递实体uesrinfo而不是参数id.username 等,这样方便对实体中参数进行调用
        Dim reader As SqlDataReader
        Dim eUser As New Model.userInfo
        Dim sql As String = "select UserName,PassWord FROM User_info WHERE UserName=@username and PassWord=@password"
        Dim cmd As New SqlCommand(sql, conn)

        cmd.CommandText = sql
        cmd.CommandType = CommandType.Text
        cmd.Parameters.Add(New SqlParameter("@username", User.UserName))
        cmd.Parameters.Add(New SqlParameter("@password", User.Pwd))
        conn.Open()
        reader = cmd.ExecuteReader
        While reader.Read()
            eUser.UserName = reader.GetString(0)
            eUser.Pwd = reader.GetString(1)
        End While
        Return eUser
        conn.Close()


    End Function
End Class
接下来是BLL层:前提引用DAL和实体类

Public Class LoginMannager
    Public Function UserLogin(ByVal User As Model.userInfo) As Model.userInfo
        Dim uDao As New DAL.user
        Dim eUser1 As New Model.userInfo

        eUser1 = uDao.SelectUser(User) '将数据传给D层

        If IsNothing(eUser1.UserName) Then
            Throw New Exception("登录失败,请验证请验证登陆名和密码!")
        Else
            MsgBox("登陆成功!")

            Return eUser1
        End If

    End Function
End Class
       引用:我们看到三层架构的主要特点:UI调用BLL中类的方法,即UI知道BLL的方法,但不知道方法实现的细节,而BLL层并不知道UI的存在.同样BLL层调用DAL,而DAL也不知道BLL的存在。三者中任何一者发生变化都不会影响其他俩者的正常工作,只对变化者进行重新调整即可。三层把Entity实体类的实例作为参数来传递.
           总之,表示层只提供软件系统与用户交互的接口;业务逻辑层是界面层和数据访问层之间的桥梁,负责数据处理和传递;数据访问层只负责数据的存取工作。


三层架构-----实践篇,布布扣,bubuko.com

三层架构-----实践篇

标签:vb.net   三层   设计   

原文地址:http://blog.csdn.net/lxy344x/article/details/28451653

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!