数据访问层返回DataTable [英] Data Access Layer returns DataTable

查看:88
本文介绍了数据访问层返回DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的问题,我刚才问过:将BLL(业务逻辑层)分解为BLL和DAL(数据访问层)



此方法(数据如果我从数据访问层返回一条记录,那么传输对象)似乎工作正常,即getNameByID返回一条记录。



如果您有一个数据访问层功能getName(),它返回许多记录,例如数千或数百万要在业务逻辑层中处理? (这是一个计划的任务)。当需要这一点时,我正在返回一个DataTable(因为数据读取器不能超过VB.NET 2008中的一个连接)。不过,这个问题和答案似乎否定了这种做法:从DAL返回DataTable或DataSet是错误的方法。这是一个糟糕的做法吗?



我意识到有一些ORM工具,如NHibernate,我打算为未来的项目使用更多。但是,我当前项目中的数据访问代码已经是由其他人写的,但是我想重新构建它。



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

 导入Microsoft.VisualBasic 

公共类PersonBLL
私人名称作为字符串
私人年龄作为整数

Dim objPersonDAL作为新PersonDAL
Dim personList作为列表(人)

'选项2
公共函数getPersonByID()作为列表(人)
personList = objPersonDAL.getPersonByID()
返回personList
结束函数



公共函数ShowMessageBox(ByVal listPersonBLL As List(Of Person))
对于每个p作为Person在listPersonBLL
Me.Age = p.Age
Me.Name = p.Name
MsgBox(Me.Age)
Ms gBox(Me.Name)
下一个
结束函数

结束类

公共类PersonDAL
私有名字作为字符串
私人年龄作为整数

公共函数getPersonByID()作为列表(人)
'连接到数据库并获取Person。返回个人对象
Dim personList As List(Of Person)=新列表(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)
返回(personList)
结束函数
结束类

公共类Person
Private _Name As String
Private _Age As Integer

公共属性名称() As String
获取
返回_Name
结束Get
Set(ByVal value As String)
_N ame = value
结束集
结束属性

公共财产年龄()As Integer
获取
返回_Age
结束获取
Set(ByVal value As Integer)
_Age = value
结束集
结束属性
结束类

公共类Form1

Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load
'如果Environment.GetCommandLineArgs(0)=Test然后
'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
结束类


解决方案

返回一个 DataTable 并不是完全可怕的 - 这样做的确更糟糕 - 这只是部分可怕的。但是,除非没有其他选择,否则谁想要吃一些可怕的食物?



所以,除非有一些原因你需要使用 DataTable ,我建议坚持使用自定义的DTO类,并且只要你的DAL返回这些对象的列表。例如:

 公共功能GetNames()作为列表(NameDto)
'...
结束功能


Please have a look at the following question, which i asked some time ago: Breaking BLL (Business Logic Layer) to BLL and DAL (Data Access Layer)

This approach (Data Transfer Object) seems to work well if I am returning one record from the data access layer i.e. getNameByID returns one record.

What happens if you have a Data Access Layer function called getName(), which returns many records e.g. thousands or millions to be processed in the Business Logic Layer? (it is a scheduled task). When this is required I am currently returning a DataTable (because data readers cannot outlive a connection in VB.NET 2008). However, this question and answer seems to negate this approach: Is returning DataTable or DataSet from DAL is wrong approach. Is this a poor approach?

I realise there are ORM tools like NHibernate, which I plan to use more for future projects. However, the data access code in my current project is already written by someone else but I want to refactor it as I go along.

Update Here is some code (as suggested by 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

解决方案

Returning a DataTable isn't completely terrible--there's certainly worse ways of doing it--it's only partly terrible. But, who wants to eat partly terrible food unless they have no other option?

So, unless there's some reason why you need to use a DataTable, I would recommend sticking with custom DTO classes and just have your DAL return a list of those objects. For instance:

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

这篇关于数据访问层返回DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆