数据访问层返回DataTable

时间:2013-01-22 20:11:35

标签: vb.net design-patterns

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

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

如果您有一个名为getName()的数据访问层函数,会返回许多记录,例如在业务逻辑层中处理数千或数百万? (这是预定的任务)。当需要这个时,我目前正在返回一个DataTable(因为数据读取器在VB.NET 2008中不能超过连接)。但是,这个问题和答案似乎否定了这种方法:Is returning 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

1 个答案:

答案 0 :(得分:2)

回归DataTable并不是完全可怕的 - 这种做法肯定更糟糕 - 它只是部分可怕。但是,除非没有别的选择,否则谁想吃一些可怕的食物?

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

Public Function GetNames() As List(Of NameDto)
    '...
End Function