Private _bgWorker As BackgroundWorker = Nothing
Private _bgWorkerMessage As String = String.Empty
Private _bgPercentComplete As Integer = 0
Private _dictQueries As Dictionary(Of String, String) = New Dictionary(Of String, String)
Public Sub New()
_dictQueries.Add("IPB", "")
_dictQueries.Add("Figure", "")
_dictQueries.Add("Part", "")
_dictQueries.Add("Tags", "")
End Sub
Public Sub New(ByRef bgWorker As BackgroundWorker)
Me.New()
_bgWorker = bgWorker
End Sub
Public Sub New(ByVal dictQueries As Dictionary(Of String, String))
Me.New()
If Not dictQueries.ContainsKey("IPB") Or Not dictQueries.ContainsKey("Figure") Or Not dictQueries.ContainsKey("Part") Or Not dictQueries.ContainsKey("Tags") Then
'I want to throw an exception/warning right here, or make it error out'
End If
_dictQueries = dictQueries
End Sub
Public Sub New(ByRef bgWorker As BackgroundWorker, ByVal dictQueries As Dictionary(Of String, String))
Me.New(bgWorker)
If Not dictQueries.ContainsKey("IPB") Or Not dictQueries.ContainsKey("Figure") Or Not dictQueries.ContainsKey("Part") Or Not dictQueries.ContainsKey("Tags") Then
'I want to throw an exception/warning right here, or make it error out'
End If
_dictQueries = dictQueries
End Sub
我的目标是创建一个可以将相同文件类型和结构导入到任何数据库的类,只要设置了连接和查询即可。我们有一个“通用”数据库模式,但每当我们创建SQLite应用程序时,都无法保证开发人员将使用相同的模式。因此,在主构造函数中,我将定义常见查询。但是如果他们定义了查询,我想确保有四个特定的查询,因为这些将是我使用的唯一查询。现在,我想在构造函数中检查它,以便开发人员可能会抓住它。用户抓住它是没有意义的,因为产品不应该被释放。那么,有没有办法检查构造函数上的参数,以确保它是“有效的”。我知道Visual Studio会自动使用类型,但我不知道是否有办法检查值以确保它们是需要的。我想要做的就是抛出一个警告,而不是让它无法构建(它们可以传入一个从数据库构建的值,谁知道)。
答案 0 :(得分:2)
我在另一个SO线程中看到过这个名为“Stringy typing”的东西。不,不可能,在运行时之前你无法找到字符串包含的内容。即使在运行时测试它的价值也是不确定的,查询语句可能会出现更多错误。您似乎正在重新发明存储过程,请考虑使用dbase引擎支持它们。
答案 1 :(得分:0)
不,我认为你能得到的最接近的是实际上强烈输入参数。创建IPBString
,FigureString
等,以便开发人员别无选择,只能传递正确的内容。这看起来有点矫枉过正。如何使用Code Contracts?