如何模拟 OleDbDataAdapter(query, connStr)

How to Mock OleDbDataAdapter(query, connStr)

如何在 FilledDataInDataTable 函数中模拟 OleDbDataAdapter(query, conn)?我可以吗? 我不知道如何处理查询和连接字符串参数的依赖关系。

Public Class DataTableOfDataFromExportCSV

   Private _adp As IOleDbDataAdapter

   Public Sub New(iadp As IOleDbDataAdapter)
       _adp = iadp
   End Sub

   Public Function FilledDataInDataTable(query As String, conStr As String) As DataTable

     Dim dt As New DataTable
     Dim adp = _adp.OleDbDataAdapter(query, conStr)
     adp.Fill(dt)

     Return dt

  End Function

End Class

实施

Public Class MyOleDbDataAdapter
 Implements IOleDbDataAdapter

 Public Function OleDbDataAdapter(query As String, conn As String) As OleDbDataAdapter Implements IOleDbDataAdapter.OleDbDataAdapter
    Dim adp As New OleDbDataAdapter(query, conn)
    Return adp

 End Function
End Class

我的界面

Public Interface IOleDbDataAdapter
   Function OleDbDataAdapter(query As String, conn As String) As OleDbDataAdapter
End Interface

How can I mock OleDbDataAdapter

你不能,因为它是 NotInheritable (sealed - c#)。

Public NotInheritable Class OleDbDataAdapter
Inherits DbDataAdapter
Implements ICloneable

我们这里有一个 Leaky Abstraction

仅公开执行所需功能明确需要的内容。

例如

Public Interface IOleDbDataAdapter
   Function Fill(query As String, dataTable As DataTable) As Integer
End Interface

这样,像 OleDbDataAdapter 这样的实现 details/concerns 就不会导致紧耦合。

Public Class DataTableOfDataFromExportCSV

    Private adp As IOleDbDataAdapter

    Public Sub New(iadp As IOleDbDataAdapter)
        adp = iadp
    End Sub

    Public Function FilledDataInDataTable(query As String) As DataTable
        Dim dt As New DataTable
        adp.Fill(query, dt)
        Return dt
    End Function

End Class

请注意,在构建组件的对象图时或在构建组件之后,删除了可以在组合根中管理的运行时数据。