1

请看一下我前段时间问过的以下问题:Breaking BLL (Business Logic Layer) to BLL and DAL (Data Access Layer)

如果我从数据访问层返回一条记录,即 getNameByID 返回一条记录,这种方法(数据传输对象)似乎效果很好。

如果您有一个名为 getName() 的数据访问层函数,该函数返回许多记录,例如要在业务逻辑层处理的数千或数百万条记录,会发生什么情况?(这是一个计划任务)。当需要这样做时,我目前正在返回一个 DataTable(因为数据读取器不能超过 VB.NET 2008 中的连接)。但是,这个问题和答案似乎否定了这种方法:Is returned DataTable or DataSet from DAL is wrong approach。这是一个糟糕的方法吗?

我意识到有像 NHibernate 这样的 ORM 工具,我计划在未来的项目中更多地使用它们。但是,我当前项目中的数据访问代码已经由其他人编写,但我想在进行过程中对其进行重构。

更新这里是一些代码(由 Stephen Doggart 建议):

Imports Microsoft.VisualBasic

        Public Class PersonBLL
            Private Name As String
            Private Age As Integer

            Dim objPersonDAL As New PersonDAL
            Dim personList As List(Of Person)

            'Option 2
            Public Function getPersonByID() As List(Of Person)
                personList = objPersonDAL.getPersonByID()
                Return personList
            End Function



    Public Function ShowMessageBox(ByVal listPersonBLL As List(Of Person))
        For Each p As Person In listPersonBLL
            Me.Age = p.Age
            Me.Name = p.Name
            MsgBox(Me.Age)
            MsgBox(Me.Name)
        Next
    End Function

        End Class

        Public Class PersonDAL
            Private Name As String
            Private Age As Integer

            Public Function getPersonByID() As List(Of Person)
                'Connect to database and get Person.  Return a person object
                Dim personList As List(Of Person) = New List(Of Person)
                Dim p1 As New Person
                p1.Name = "Ian"
                p1.Age = 30
                personList.Add(p1)
                Dim p2 As New Person
                p2.Name = "Steven"
                p2.Age = 28
                personList.Add(p2)
                Dim p3 As New Person
                p3.Name = "Sharon"
                p3.Age = 29
                personList.Add(p3)
                Return (personList)
            End Function
        End Class

        Public Class Person
            Private _Name As String
            Private _Age As Integer

            Public Property Name() As String
                Get
                    Return _Name
                End Get
                Set(ByVal value As String)
                    _Name = value
                End Set
            End Property

            Public Property Age() As Integer
                Get
                    Return _Age
                End Get
                Set(ByVal value As Integer)
                    _Age = value
                End Set
            End Property
        End Class

    Public Class Form1

            Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                'If Environment.GetCommandLineArgs(0) = "Test" Then
                'MsgBox("Test")
                'End If
                Dim p1 As PersonBLL = New PersonBLL
                Dim p2 As List(Of Person) = p1.getPersonByID()
                Dim p3 As PersonBLL = New PersonBLL
                p3.ShowMessageBox(p2)
            End Sub
        End Class
4

1 回答 1

2

返回 aDataTable并不完全糟糕——当然还有更糟糕的方法——它只是部分糟糕。但是,除非他们别无选择,否则谁愿意吃部分糟糕的食物呢?

因此,除非出于某种原因您需要使用 a ,否则DataTable我建议您坚持使用自定义 DTO 类并让您的 DAL 返回这些对象的列表。例如:

Public Function GetNames() As List(Of NameDto)
    '...
End Function
于 2013-01-22T20:23:02.163 回答