-1
我有一個遺留應用程序,它包含一個數據庫輔助類。客戶端調用下面的函數:需要可選參數
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屬性不重構的方法代碼顯着。這個函數被客戶端調用很多次 - 只有一個情況需要改變commandtimeout。我有一些快速的想法是:
1)將commandtimeout設置爲一個實例變量(它不是一個靜態的 類)。這似乎不正確,但。
2)使用可選 與參數的默認值:30。
是否有這樣做的任何其他方式?
我意識到我可能應該使用ORM,但這是一個遺留應用程序。
使用可選參數時出現什麼問題? –
@Matt Wilko,我相信你不能在相同的函數簽名中使用參數數組和可選參數。 – w0051977
也許你可以使用相同的名字創建第二個函數,但是增加了超時參數。 –