私の現在のアプリケーションは、インスタンスベースのデータアクセス層を使用しています。接続文字列を使用してレイヤーをインスタンス化します。次に、ある種のコマンドを実行するメソッドを呼び出します。たとえば、データセットを埋めるメソッドがあります。基本的に、ストアドプロシージャとSQLパラメータを渡して、データセットを取得します。データアクセスまたはインスタンスベースを処理するための静的クラスがある方が良いですか?私はオブジェクトを含むドメインレイヤーを持っていますが、ORMのようにオブジェクトをマッピングしていません。オブジェクトをファクトリに渡し、ファクトリがデータレイヤーをインスタンス化してデータセットをプルバックします。次に、データセットをオブジェクトにマップします。アプリを更新する予定ですが(もちろんC#に移行する予定です)、全体を変更する時間がありません。挿入の更新と削除についても同じことを行います。今のところ大丈夫なら教えてください。このアプローチに問題はありますか?そうでなければ、私は何をすべきですか?私はこのクラスを書きませんでした。私はそれをオンラインで見つけ、これが私が必要としているものだと思いました。
データクラスの例を次に示します。
Public Sub New(ByVal connectionString As String)
_connectionString = connectionString
End Sub
Public Function FillDataset(ByVal cmd As String, ByVal cmdType As CommandType, Optional ByVal parameters() As SqlParameter = Nothing) As DataSet
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim sqlda As SqlDataAdapter = Nothing
Dim res As New DataSet
Try
connection = New SqlConnection(_connectionString)
command = New SqlCommand(cmd, connection)
command.CommandType = cmdType
AssignParameters(command, parameters)
sqlda = New SqlDataAdapter(command)
sqlda.Fill(res)
Catch ex As Exception
'CreateDataEntry(ex, WriteType.ToFile, cmd)
Finally
If Not (connection Is Nothing) Then connection.Dispose()
If Not (command Is Nothing) Then command.Dispose()
If Not (sqlda Is Nothing) Then sqlda.Dispose()
End Try
Return res
End Function
Public Function ExecuteNonQuery(ByVal spname As String, ByVal ParamArray parameterValues() As Object) As Object
Dim connection As SqlConnection = Nothing
Dim command As SqlCommand = Nothing
Dim res As Object = Nothing
Try
connection = New SqlConnection(_connectionString)
command = New SqlCommand(spname, connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.AddRange(parameterValues)
connection.Open()
command.ExecuteNonQuery()
res = command.Parameters(command.Parameters.Count - 1).Value
Catch ex As Exception
CreateDataEntry(ex, WriteType.ToFile, spname)
If Not (transaction Is Nothing) Then
transaction.Rollback()
End If
Finally
If Not (connection Is Nothing) AndAlso (connection.State = ConnectionState.Open) Then connection.Close()
If Not (command Is Nothing) Then command.Dispose()
End Try
Return res
End Function