【问题标题】:Optional parameter needed需要可选参数
【发布时间】:2014-01-30 09:59:17
【问题描述】:

我有一个遗留应用程序,其中包含一个数据库助手类。客户端调用如下函数:

 Public Overloads Shared Function ExecuteReader(ByVal connection As DbConnection, _
                                                           ByVal commandType As CommandType, _
                                                           ByVal commandText As String, _
                                                           ByVal ParamArray commandParameters() As DbParameter) As DbDataReader
                ' Pass through the call to private overload using a null transaction value
                Return ExecuteReader(connection, CType(Nothing, DbTransaction), commandType, commandText, commandParameters, dbConnectionOwnership.External)

End Function

如您所见,上面的函数调用了一个重载的 ExecuteReader,详细说明如下:

Private Overloads Shared Function ExecuteReader(ByVal connection As DbConnection, _
                                                            ByVal transaction As DbTransaction, _
                                                            ByVal commandType As CommandType, _
                                                            ByVal commandText As String, _
                                                            ByVal commandParameters() As DbParameter, _
                                                            ByVal connectionOwnership As dbConnectionOwnership) As DbDataReader

            If (connection Is Nothing) Then Throw New ArgumentNullException("connection")

            Dim mustCloseConnection As Boolean = False
            Dim cmd As DbCommand
            ' Create a command and prepare it for execution
            If TypeOf (connection) Is SqlConnection Then
                cmd = New SqlCommand
            ElseIf TypeOf (connection) Is OracleConnection Then
                cmd = New OracleCommand
            End If
            Try
                ' Create a reader
                Dim dataReader As DbDataReader

                PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, mustCloseConnection)
                ' Call ExecuteReader with the appropriate CommandBehavior
                If connectionOwnership = dbConnectionOwnership.External Then
                    dataReader = cmd.ExecuteReader()
                Else
                    dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
                End If

                ' Detach the SqlParameters from the command object, so they can be used again
                Dim canClear As Boolean = True
                Dim commandParameter As DbParameter
                For Each commandParameter In cmd.Parameters
                    If commandParameter.Direction <> ParameterDirection.Input Then
                        canClear = False
                    End If
                Next

                If (canClear) Then cmd.Parameters.Clear()

                Return dataReader
            Catch
                If (mustCloseConnection) Then connection.Close()
                Throw
            End Try
        End Function

如果不显着重构代码,我看不到设置 CommandTimeout 属性的方法。客户端多次调用此函数 - 只有一种情况需要更改命令超时。我的一些快速想法是:

1) 将 commandtimeout 设置为实例变量(它不是静态的 班级)。不过,这似乎不正确。
2)使用可选的 参数,默认值为:30。

还有其他方法吗?

我意识到我可能应该使用 ORM,但这是一个遗留应用程序。

【问题讨论】:

  • 使用可选参数有什么问题?
  • @Matt Wilko,我相信你不能在同一个函数签名中使用参数数组和可选参数。
  • 也许您可以创建一个具有相同名称但添加超时参数的第二个函数。

标签: .net vb.net ado.net


【解决方案1】:

我会使用可选参数,但不是使用硬编码的默认值 30 秒,而是使用默认值 -1 并添加额外的 if 语句来检查是否提供了特定值以及是否默认超时应该被覆盖。

通过这种方式,您仍然可以在连接字符串中设置默认超时,并在需要时使用特定的超时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-17
    • 2011-10-16
    • 2020-08-24
    • 2016-07-27
    • 1970-01-01
    • 2012-06-15
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多