三层之常用返回值

来源:互联网 发布:阿里云虚拟主机 php 编辑:程序博客网 时间:2024/05/16 07:01
我接触三层架构已经有一段时间了,刚开始接触三层架构的时候,有两个问题困扰了我很长时间,第一个是三层应该如何分层呢?第二个问题就是三层返回值如何设置!
提高班的学习方法是项目驱动,后来开始接触用三层写的机房收费系统,对三层也开始慢慢的理解了,慢慢了解了三层的返回值在什么情况下,我该如何返回值,三层架构的返回值一般情况分为以下三种:
一.返回数据表Datatable 或者是数据集Dataset
无论是返回数据集Dataset还是数据表Datatable ,性质都是一样的,使用的情况就是在你需要多条数据的时候,就返回数据表,但是多条数据不放在一个表中,那么这个时候你就可以返回Dataset了,已返回一个数据表为例,我们来看看代码:
D层:
 ''' <summary>    ''' 值班老师    ''' </summary>    Public Function SelectDutyTeacher(ByVal AllThdt As DataTable) As DataTable        Dim sqlcmdstr As String = "select RecordNo as 记录号,UserName  as 用户名,UseDate  as 登录日期,UseTime as 登录时间 ,RoomNo  as 机器名 from WorkRecord where NouseDate is null"        Dim table As DataTable        table = SqlHelp.Sqlhelper.SelectInfo(sqlcmdstr, CommandType.Text)        Return table    End Function

B层:
 ''' <summary>    ''' 正值班教师    ''' </summary>    ''' <param name="TeacherInfo"></param>    ''' <returns>table</returns>    ''' <remarks>正值班教师表</remarks>    Public Function DutyTeacher(ByVal TeacherInfo As DataTable) As DataTable        Dim table As New DataTable        Dim DalTeacher As New CRCMDAL.DutyTeacherDAL        '调用D层DutyTeacherDAL类的SelectDutyTeacher方法获取正在值班教师表        table = DalTeacher.SelectDutyTeacher(table)        Return table    End Function

U层:
 Dim BllTeacher As New CRCMBLL.DutyTeacherBLL        Dim Table As New DataTable        '先对DataGridView1中的数据进行清除,刷新        DataGridView1.DataSource = DBNull.Value        DataGridView1.Refresh()        '调用B层DutyTeacherBLL类的DutyTeacher方法,获取正在值班教师        Table = BllTeacher.DutyTeacher(Table)



二.返回实体
我们都知道,三层的传值是通过实体传递的,因为实体遵从了面向对象设计的开闭原则,如果你的程序集中,返回的值有且仅有一条记录的时候,你就可以返回实体了,这样就比较方便,
界面:

D层:
''' <summary>    ''' 通过卡号查找相关信息    ''' </summary>    ''' <param name="StudentInfo"></param>    ''' <returns>EntityStudent</returns>    ''' <remarks>返回学生的基本信息实体</remarks>    Public Function selectStudent(ByVal StudentInfo As StudentBasicInfoEntity) As StudentBasicInfoEntity        Dim EntityStudent As New CRCMEntity.StudentBasicInfoEntity        Dim sqlcmdstr As String = "select *  from StudentBasicInfo where StudentCardID =@StudentCardID"        Dim paras As SqlParameter() = {New SqlParameter("@StudentCardID", StudentInfo.StudentCardID)}        Dim table As DataTable        table = SqlHelp.Sqlhelper.SelectInfo(sqlcmdstr, CommandType.Text, paras)        EntityStudent.StudentCardID = table.Rows(0).Item("StudentCardID")        EntityStudent.StudentID = table.Rows(0).Item("StudentID")        EntityStudent.StudentName = table.Rows(0).Item("StudentName")        EntityStudent.StudentSex = table.Rows(0).Item("StudentSex")        EntityStudent.Department = table.Rows(0).Item("Department")        EntityStudent.Studenttype = table.Rows(0).Item("Studenttype")        EntityStudent.Cash = table.Rows(0).Item("Cash")        EntityStudent.Grade = table.Rows(0).Item("Grade")        EntityStudent.ClassNo = table.Rows(0).Item("ClassNo")        EntityStudent.Marks = table.Rows(0).Item("Marks")        Return EntityStudent    End Function


B层:
 
''' <summary>    ''' 查找学生的基本信息    ''' </summary>    ''' <param name="StudentInfo"></param>    ''' <returns>EntityStudent</returns>    ''' <remarks>学生基本信息实体</remarks>    Public Function SelectStudentInfo(ByVal StudentInfo As StudentBasicInfoEntity) As StudentBasicInfoEntity        Dim EntityStudent As New CRCMEntity.StudentBasicInfoEntity        Dim DalStudent As New CRCMDAL.ModifyStudentInfoDAL        EntityStudent.StudentCardID = StudentInfo.StudentCardID        EntityStudent = DalStudent.selectStudent(EntityStudent)        Return EntityStudent


U层:
''' <summary>    '''显示学生基本信息    ''' </summary>    Public Function ShowStudentInfo() As Boolean        Dim EntityStudent As New CRCMEntity.StudentBasicInfoEntity        Dim BllStudent As New CRCMBLL.ModifyStudentBll        txtStudentCardID.Text = FrmStuBasicModify.StudentCardID        '卡号        EntityStudent.StudentCardID = txtStudentCardID.Text        '调用B层的SelectStudentInfo方法获取全部学生信息        EntityStudent = BllStudent.SelectStudentInfo(EntityStudent)        txtStudentCardID.Text = EntityStudent.StudentCardID        '学号        txtStudentID.Text = EntityStudent.StudentID        '性别        txtStudentSex.Text = EntityStudent.StudentSex        '姓名        txtStudentName.Text = EntityStudent.StudentName        '系别        txtDepartment.Text = EntityStudent.Department        '年级        txtGrade.Text = EntityStudent.Grade        '班级        txtClassNo.Text = EntityStudent.ClassNo        '金额        txtCash.Text = EntityStudent.Cash        '卡类型        txtStudenttype.Text = EntityStudent.Studenttype        '备注        txtMarks.Text = EntityStudent.Marks        Return True    End Function



三.返回Boolen
当我们对数据进行增,删,改的时候,对执行ExecuteNonQuery命令, ExecuteNonQuery返回的是行数,就可以更加ExecuteNonQuery返回行数判断是否对数据库表的增,删,改是否成功了!
界面:

D层:
 ''' <summary>    ''' 更改学生的基本信息    ''' </summary>    ''' <param name="StudentInfo"></param>    ''' <returns>Boolean</returns>    ''' <remarks>修改成功返回True,不成功返回false</remarks>    Public Function InsertStudent(ByVal StudentInfo As StudentBasicInfoEntity) As Boolean        'Dim EntityStudent As New CRCMEntity.StudentBasicInfoEntity        Dim sqlcmdstr As String = "PROC_CR_ModifyStudentBasic"        Dim paras As SqlParameter() = {New SqlParameter("@StudentCardID", StudentInfo.StudentCardID),                                       New SqlParameter("@StudentID", StudentInfo.StudentID),                                     New SqlParameter("@StudentName", StudentInfo.StudentName),                                     New SqlParameter("@StudentSex", StudentInfo.StudentSex),                                     New SqlParameter("@Department", StudentInfo.Department),                                     New SqlParameter("@Grade", StudentInfo.Grade),                                     New SqlParameter("@ClassNo", StudentInfo.ClassNo),                                     New SqlParameter("@Marks", StudentInfo.Marks)                                    }        Dim res As Integer  '定义受影响的行数        res = SqlHelp.Sqlhelper.ExecuteNoQuery(CommandType.StoredProcedure, sqlcmdstr, paras)        '返回收影响的行数为-1,则返回true,反之返回False        If res < 0 Then            Return True        Else            Return False        End If    End Function

B层:
    ''' <summary>    ''' 把修改的学生基本信息插入数据库表中    ''' </summary>    ''' <param name="StudentInfo"></param>    ''' <returns>Boolean</returns>    ''' <remarks>更新成功返回True,不成功返回False</remarks>    Public Function InsertStudent(ByVal StudentInfo As StudentBasicInfoEntity) As Boolean        Dim DalStudent As New CRCMDAL.ModifyStudentInfoDAL        '判断是否更新成功,若更新成功返回True,反之返回false        If DalStudent.InsertStudent(StudentInfo) Then            Return True        Else            Return False        End If    End Function

U层:
    ''' <summary>    '''确定修改    ''' </summary>    Private Sub btnModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModify.Click        Dim EntityStudent As New CRCMEntity.StudentBasicInfoEntity        Dim BllStudent As New CRCMBLL.ModifyStudentBll        '卡号        EntityStudent.StudentCardID = txtStudentCardID.Text        '学号        EntityStudent.StudentID = txtStudentID.Text        '性别        EntityStudent.StudentSex = txtStudentSex.Text        '姓名        EntityStudent.StudentName = txtStudentName.Text        '系别        EntityStudent.Department = txtDepartment.Text        '年级        EntityStudent.Grade = txtGrade.Text        '班级        EntityStudent.ClassNo = txtClassNo.Text        '备注        EntityStudent.Marks = txtMarks.Text        '调用B层的InsertStudent方法,插入修改信息        If BllStudent.InsertStudent(EntityStudent) Then            MsgBox("修改成功!", vbOK, "温馨提示")        End If    End Sub

在做机房收费系统的时候,和进度差不多的人交流了一下,也有些人比较喜欢倾向于返回的值全部是datatable或者是dataset,在B层或C层需要的时候再取,在进行值的比较处理,无论你用那种返回值,都是没有错误的,但是根据不同的情况,返回不同的值,就会相对的提高我们的效率!

原创粉丝点击