Executing a list of tasks from a database

I have a list of tasks I get from my database (as strings) and then execute. However I'm uncertain on how to implement this correctly.

Currently I have implemented this in the following way:

Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
If param.Value Then
RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID

Trace.TraceInformation(monitorTasks.Count() & " have been found.")
'Perform each monitor task and save the results to the database
Try

Dim result = String.Empty

Case "getlastbackup"
Case "getamosrtversion"
result = General.AmosRTVersion
Case "getstockondummypercentage"
Case "getnumberofhistoryrecords"
Case "getnumberofsparepartlogrecords"
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
Case "getdifferencesofcompjobnextdueandworkorderdue"
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
Case "getactivecompjobswithnextdue"
Case "getactivecompjobswithoutnextdue"
Case "getopenworkorders"
Case "getamosguiversion"
Case "checkamosmobileactivation"
Case "checkchangelogactivation"
Case "retrievejdnmodules"
Case "retrieveamosparameters"
Case "retrievestocksituation"
Case "executesql"
Case "getfreespace"
Case Else
ExceptionMonitor.LogExceptionEvent(
End Select

If Not String.IsNullOrEmpty(result) Then

Else
ExceptionMonitor.LogExceptionEvent(
End If

Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End If
Next
End Sub


I am certain there is a better way to do this. I did some reading and always end up with the Strategy pattern (like asked here) but I don't seem to understand how to implement it in this situation.

Am I going in the correct direction here or not at all?

Important: Always set Option Explicit On

Option Strict "restricts implicit data type conversions to only widening conversions". See [here][2]. With this option enabled, you can't accidentally convert one data type to another that is less precise (e.g. from an Integer to a Byte). Again, an option that should be turned on by default.

General

• by using guard conditions you can save horizontal spacing which improves readability. A guard condition is a condition which guards the code from beeing executed if one ore more certain conditions are fullfilled. This is done by either returning at this point or by throwing an exception depending on which is suitable in this case.

• extract the processing of param to a separate method.

• extract the processing of monitorTask to a separate method.
• extract the processing of the result to a separate method.

• comments should describe why something is done. What is done should be described by the code itself.

• calling ToString() on a String is not necessary.

this would lead for your Monitoringmethod to look like

Private Sub Monitoring(installationCode As String, drive As String, syncFolder As String)
InitializeDatabaseConnection(installationCode)
TraceInitialize(drive, "Monitoring")
Trace.TraceInformation("Monitoring started for installation: " & installationCode)
If Not param.Value Then continue For
ProcessParameter(param)
Next
End Sub

Private Sub ProcessParameter(param As someType, installationCode As String, drive As String, syncFolder As String)

RemoteTask.Deptid = Department.GetDepartmentsNotInOne().Where(Function(x) x.Code = "001").FirstOrDefault.DepartmentID

Trace.TraceInformation(monitorTasks.Count() & " have been found.")

Try
Catch ex As Exception
Trace.TraceInformation("Error " + ex.ToString())
ExceptionMonitor.LogExceptionEvent(ex, ProblemSeverity.Error)
End Try
Next
End Sub

Private Function ProcessMonitorTasks(monitorTaskDescription As String, installationCode As String, drive As String, syncFolder As String) As String

Case "getlastbackup"
Case "getamosrtversion"
return  General.AmosRTVersion
Case "getstockondummypercentage"
Case "getnumberofhistoryrecords"
Case "getnumberofsparepartlogrecords"
Case "getinconsistenciesbetweencompjobnextdueandlastdone"
Case "getdifferencesofcompjobnextdueandworkorderdue"
Case "getinconsistenciesbetweencompjobsprioandworkordersprio"
Case "getactivecompjobswithnextdue"
Case "getactivecompjobswithoutnextdue"
Case "getopenworkorders"
Case "getamosguiversion"
Case "checkamosmobileactivation"
Case "checkchangelogactivation"
Case "retrievejdnmodules"
Case "retrieveamosparameters"
Case "retrievestocksituation"
Case "executesql"
Case "getfreespace"
Case Else
ExceptionMonitor.LogExceptionEvent(
End Select
return String.Empty
End Function

Private Sub ProcessMonitorResult(result As String, monitorTask As someObject, installationCode As String)

If String.IsNullOrEmpty(result) Then
ExceptionMonitor.LogExceptionEvent(
Exit Sub
End If

End Sub


Update:

I really don't like typing strings in code. I thought about using an Enum but I'm not entirely sure how that would work, any ideas?

This just depends on how monitorTask can/should be changed. If you want to use enums, the monitorTask will need to provide this enum too. Right now you are using the Description property which is a string.

If you want to/have to stick to the "string version" you should at least delare constants and use them in the Select..Case and whereever you need them.

• Correct me if I'm wrong, but the reason for switching up the select-case (among other things) is because the returns are going to prevent it from checking all cases after the one that succeeds (as the original is lacking breaks), Yes? If that is the case, I figured it was worth mentioning in the off-case that the OP was not aware of this reasoning or failed to realize. – Shelby115 Jan 16 '15 at 16:58
• In Vb.NET there is no slipping through the cases. If the desired case is found the Select will be left. – Heslacher Jan 16 '15 at 17:02
• Thanks a lot! This has been a great help. What exactly are guard conditions? Google has lead me to AndAlso but I don't believe that's correct? What I'm still wondering is if there isn't a better way to do that big Select Case? I really don't like typing strings in code. I thought about using an Enum but I'm not entirely sure how that would work, any ideas? :) Thanks again! – Schoof Jan 17 '15 at 11:16
• Updated answer. – Heslacher Jan 18 '15 at 11:38
• @Thomas you may have better luck googling for "guard clause" instead ;) – Mathieu Guindon Jan 18 '15 at 14:41