DDi*_*ita 6 vb.net data-access-layer .net-3.5
我当前的应用程序使用基于实例的数据访问层.我用连接字符串实例化图层.然后我调用一个可以执行某种命令的方法.例如,有一种方法可以填充数据集.基本上,我传递存储过程和任何SQL参数并获取数据集.有一个静态类来处理我的数据访问或基于实例的更好吗?我确实有一个带有对象的Domain层,但我没有像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
Run Code Online (Sandbox Code Playgroud)
首先,我认为基于实例的方法是正确的.使用静态类会使单元测试其他类时单元测试DAL和模拟DAL变得更加困难.其次,我认为你应该重新考虑建立自己的DAL.通过使用现有的(经过良好测试的)ORM(如LINQtoSQL,nHibernate,nTier,甚至实体框架),您将花费大量时间来创建,维护和测试DAL - 花费更多时间直接有益于您的业务需求的代码.在我的LINQtoSQL案例中,我已经完成了手工构建的DAL和ORM,并且我发现我花费更少的时间来测试(和修复)我的DAL进入ORM路由.
归档时间: |
|
查看次数: |
3015 次 |
最近记录: |