处理自定义数据访问层引用
我们的应用程序几乎完全使用自定义 DataAccessLayer 类,并且在其中我们使用数据访问应用程序块(当前版本 2)。我们偶尔会遇到臭名昭著的“GetOrdinal”错误。我们没有使用方法外的连接。我们正在使用 DAAB 版本 2。下面是我们的 DAL 方法的典型示例:
Public Function MyDALMethod(ByVal Param1 As Integer, ByVal Param2 As Integer) As System.Data.IDataReader
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = "usp_MyProcedure"
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
db.AddInParameter(dbCommand, "Param1", DbType.Int32, MyParam1)
db.AddInParameter(dbCommand, "Param2", DbType.Int32, MyParam2)
Return db.ExecuteReader(dbCommand)
End Function
在我们的代码中,我们只是实例化一个 DAL 变量并调用所需的方法。使用 DataReader 后,引用代码将关闭、处置并将读取器设置为空。但是,没有对 DAL 的引用进行任何操作。我想知道这是否是我们问题的一部分。典型的方法会像这样使用我们的 DAL:
Dim DAL as New DataAccessLayer
Dim dbReader as system.data.idatareader
dbreader = DAL.MyDALMethod(parm1, parm2)
While dbreader.read
i = dbreader.items("column1")
j = dbreader.items("column2")
End While
dbreader.close()
dbreader.dispose()
dbreader = nothing
我的主要问题是这些 DAL 引用是否应该以某种方式处理?这是一个用 VB.NET 编写的自定义类,因此它没有实现 IDisposable,所以我不确定是否需要做任何事情,但我们确实有错误和问题(例如 GetOrdinal 问题),这些错误和问题似乎是加载的-相关,我想知道这是否是问题的一部分。
Our application uses a custom DataAccessLayer class almost exclusively, and within that we do use Data Access Application Block (currently version 2). We are getting the infamous "GetOrdinal" error sporadically. We are not using out-of-method connections. We are using DAAB version 2. Below is a typical example of our DAL methods:
Public Function MyDALMethod(ByVal Param1 As Integer, ByVal Param2 As Integer) As System.Data.IDataReader
Dim db As Database = DatabaseFactory.CreateDatabase()
Dim sqlCommand As String = "usp_MyProcedure"
Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand)
db.AddInParameter(dbCommand, "Param1", DbType.Int32, MyParam1)
db.AddInParameter(dbCommand, "Param2", DbType.Int32, MyParam2)
Return db.ExecuteReader(dbCommand)
End Function
In our code we just instantiate a DAL var and call the desired method. After using the DataReader, the referencing code will close, dispose, and set the reader to nothing. However, nothing is done with the reference to the DAL. I've wondered if this is part of our problem. A typical method would use our DAL like this:
Dim DAL as New DataAccessLayer
Dim dbReader as system.data.idatareader
dbreader = DAL.MyDALMethod(parm1, parm2)
While dbreader.read
i = dbreader.items("column1")
j = dbreader.items("column2")
End While
dbreader.close()
dbreader.dispose()
dbreader = nothing
My main question is should these DAL references be disposed somehow? It's a custom class written in VB.NET, so it doesn't implement IDisposable so I'm not sure if there's anything to be done or not, but we do have errors and issues (like the GetOrdinal problem) which seem to be load-related, and I'm wondering if this is part of the problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 DAL 至少拥有一个 Disposable 成员变量(实现 IDisposable),那么它也应该实现 IDisposable。然后,这将向 DAL 的客户端指示应该调用 Dispose()。然后,DAL 的 Dispose() 将调用成员变量 Dispose()。
这是实现 IDisposable 的一般指南。
顺便说一句 - 没有必要
dbreader = Nothing
- 它什么也实现不了。请参阅 Eric Lippert 的帖子If the DAL holds at least one member variable that is Disposable (implements IDisposable), then it too should implement IDisposable. This would then indicate to DAL's client that Dispose() should be called. The DAL's Dispose() would then call the member variable(s) Dispose().
That is the general guidance for implementing IDisposable.
BTW - there is no need to
dbreader = nothing
- it achieves nothing. See Eric Lippert's post